Data transmission and sharing of main thread and sub-threading of c++11 multi-thread threads

Source: Internet
Author: User

There are many types of data transfer and sharing between C + + threads, and using global variables is a common one, let's take a look at the most basic examples of using global variables for data sharing:

#include <thread>
#include <iostream>
using namespace std;
static int id = 1;
void Autoincrease () {
	id=id+1;
}
int main ()
{
	cout << "before increase" << ID << endl;
	Thread Mythread (autoincrease);
	Mythread.join ();
	cout << "After increase" << ID << endl;
	
	return 0;
}



The main thread can get the data after the child thread processing, but this method is too simple, in the process of transmission processing, we will not only want to deal with an int type of data, if a struct, what should be done.

Next, try to transfer and process the data for a class type.

First of all, we need to define the transport class message, a relay interface receiveinterface when we need to transfer the class type to the child threading process.




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.