C++thread Learning (2) a way to communicate between threads

Source: Internet
Author: User

The following is an example of this:

Existing Class A,

The structure looks like the following:

Class A:
{...
		.
      . Some data members and member functions;
	   DataType data;
	  Main function process (datatype& data,...); /data requires other threads to be fed, and the process executes once as soon as the data is updated, depending on the system requirements.
};
Now we want class A to communicate with other threads after the thread is turned on.

First, the communication must have a common data buffer, such as the B-thread to Cheng data to a line, the basic idea is that B-thread has data into a data buffer, and then a then from the data buffer to read data. Since the data buffer is shared by two threads, the data buffer must be locked. Lock in order to avoid a situation where the B thread is writing to the data buffer, and at that point A is read from the data cache.

With this idea, we are now transforming Class A.

Class A:
{
...
Some data members and member functions;
DataType data;
Main function process (datatype& data,...); /data requires other threads to be sent
into void run ()//To define a dead loop in the class that performs the function (method)
 void Receivedatafromotherthread of this class (datatype& DataIn);//define function to receive data
 std::queue<datatype> Datacache;  Communication Data buffer
 Std::mutex Datacachemutex;       The lock std::condition_variable Cond_ of the communication data buffer
 ;   The thread's condition variable
};

Where the function that receives the data is defined in this way

void A::receivedatafromotherthread (datatype& datain)
{
	std::unique_lock<mutex> lock ( Datacachemutex);
	Datacache.push (DataIn);//Other threads put data
	cond_.notify_one () to the buffer;
}

The run function is the main function of the entire class, which defines:

void A::run ()
{while
	(1)
	{
	    {
			std::unique_lock<mutex> lock (Datacachemutex);
		      while (Datacache.size () <0) 
			  cond_.wait (lock);
			  Data=datacache.back ();//This thread reads the data from the buffer
			  datacache.pop ();
		} 
	...
	Process (data,....);
	...	
	}
}

We add so much to the purpose of the thread communication.

The transformation is complete, how to communicate it. Now for the simplest example, for example, in the main thread (the thread of the main function) and the newly defined thread to complete the communication:

Main ()
{
      a *a=new a (...); /Instantiate Class A
	  Std::thread * threada=new thread (&a::run,a);//Start a thread main loop with the run function of Class A
	  {
	  DataType DATA
	 ...
	 A->receivedatafromotherthread (data),//main thread to the Threada threads sent ...  
	  }
}

It is also simple to want to communicate with other threads and Threada, which is to pass the pointer of object A to the thread to be communicated, calling the

A->receivedatafromotherthread (data);//Other threads send the Threada thread



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.