Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #47505

Re: thread concurancy

From ruben safir <ruben@mrbrklyn.com>
Newsgroups comp.lang.c++
Subject Re: thread concurancy
Date 2016-12-21 23:48 -0500
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <o3flum$ku0$1@reader1.panix.com> (permalink)
References <o3eprl$1pf$2@reader1.panix.com> <OfqdnfvRg5oya8fFnZ2dnUU78KHNnZ2d@giganews.com>

Show all headers | View raw


How is this fix ;)


/*
 * =====================================================================================
 *
 *       Filename:  test.cpp
 *
 *    Description:  Threading Experiment
 *
 *        Version:  1.0
 *        Created:  12/18/2016 12:46:51 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Ruben Safir (mn), ruben@mrbrklyn.com
 *        Company:  NYLXS Inc
 *
 * =====================================================================================
 */

#include <iostream>
#include <thread>
#include <mutex>
std::mutex medco;
std::mutex master;

namespace testing{
	std::thread t[10];
	class PIC
	{
		public:
		PIC():source_index{&source[0]} 
		{
			canvas_index = canvas;
			std::cout << "Testing Source" << std::endl;
			for(int i = 0; i<100; i++)
			{
				std::cout << i << " " << source[i] << std::endl ;
			}

			for(int i = 0; i < 10;i++)
			{
			t[i] = std::thread([this]{ readin(); });		
			std::cerr << i << ": Making a thread"  << std::endl; 
			}
		};

		
		~PIC()
		{
			std::cerr << "In the destructor"  << std::endl;
			for(int i=0; i<10; i++)
			{
				t[i].join();
			std::cerr << i << ": Joining a thread"  << std::endl; 
			}

		};
		
		void readin()
		{
			char * loc_canvas_index;
			char * loc_source_index;
			sync_canvas_and_input(loc_canvas_index, loc_source_index);
			
			for( int i = 9; i>=0; i-- )
			{
				*loc_canvas_index = loc_source_index[i];
				std::cerr << i << ": Copy " << loc_source_index[i] << std::endl;
				std::cerr << i << ": Copied to loc_canvas_index " << reinterpret_cast<char>(*loc_canvas_index) << std::endl;
				loc_canvas_index++;
			}

		};
		void sync_canvas_and_input(char * &loc_canvas_index, char * &loc_source_index )
		{
			std::cout << "**LOCKING**" << std::endl;
			std::lock_guard<std::mutex> turn(medco);
			loc_canvas_index = canvas_index;
			loc_source_index = source_index;
			source_index += 10;
			canvas_index += 10;
		};

		char * get_canvas()
		{
			return canvas;
		}
		char * get_canvas_index()
		{
			return canvas_index;
		}
		char * get_source_index()
		{
			return source_index;
		}

		private:
		char * canvas = new char[100]{
			'z', 'z','z','z','z','z','z','z','z','z',
			'z', 'z','z','z','z','z','z','z','z','z',
			'z', 'z','z','z','z','z','z','z','z','z',
			'z', 'z','z','z','z','z','z','z','z','z',
			'z', 'z','z','z','z','z','z','z','z','z',
			'z', 'z','z','z','z','z','z','z','z','z',
			'z', 'z','z','z','z','z','z','z','z','z',
			'z', 'z','z','z','z','z','z','z','z','z',
			'z', 'z','z','z','z','z','z','z','z','z',
			'z', 'z','z','z','z','z','z','z','z','z'
		};
		char * canvas_index;
		char source[100] = {
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'
		};
		char * source_index;
	};
}//end namespace

int main(int argc, char** argv)
{
	testing::PIC fido;
	for(int i = 0; i<100;i++)
	{
		std::cout << i << " Canvas Position " << fido.get_canvas()[i] << std::endl;
	}

	return 0;
}

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

thread concurancy Popping mad <rainbow@colition.gov> - 2016-12-21 20:48 +0000
  Re: thread concurancy Paavo Helde <myfirstname@osa.pri.ee> - 2016-12-21 23:39 +0200
    Re: thread concurancy ruben safir <ruben@mrbrklyn.com> - 2016-12-21 23:48 -0500
      Re: thread concurancy Paavo Helde <myfirstname@osa.pri.ee> - 2016-12-22 12:32 +0200
        Re: thread concurancy ruben safir <ruben@mrbrklyn.com> - 2016-12-22 12:13 -0500
          Re: thread concurancy Paavo Helde <myfirstname@osa.pri.ee> - 2016-12-22 20:29 +0200
        Re: thread concurancy scott@slp53.sl.home (Scott Lurndal) - 2016-12-23 14:31 +0000
          Re: thread concurancy Paavo Helde <myfirstname@osa.pri.ee> - 2016-12-23 20:19 +0200
            Re: thread concurancy scott@slp53.sl.home (Scott Lurndal) - 2016-12-23 22:39 +0000
              Re: thread concurancy Paavo Helde <myfirstname@osa.pri.ee> - 2016-12-24 01:31 +0200
    Re: thread concurancy ruben safir <ruben@mrbrklyn.com> - 2016-12-21 23:49 -0500
      Re: thread concurancy "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-12-23 15:19 -0800
  Re: thread concurancy Sal LO <gegefffffff@gmail.com> - 2016-12-22 07:04 -0800
    Re: thread concurancy ruben safir <ruben@mrbrklyn.com> - 2016-12-22 11:00 -0500

csiph-web