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.