Turn from: C + + uses the thread class for multithreaded programming
A thread class for multithreaded operations is introduced in C++11, and the following is a simple demonstration of how to use it, and if multithreading is synchronized.
#include <iostream>#include<thread>#include<Windows.h>using namespacestd; voidthread01 () { for(inti =0; I <5; i++) {cout<<"Thread Working! "<<Endl; Sleep ( -); } } voidthread02 () { for(inti =0; I <5; i++) {cout<<"Thread is working! "<<Endl; Sleep ( $); } } intmain () {thread task01 (THREAD01); Thread task02 (THREAD02); Task01.join (); Task02.join (); for(inti =0; I <5; i++) {cout<<"Main thread is working! "<<Endl; Sleep ( $); } System ("Pause"); }
Output:
- Thread detach does not block the main thread
Two sub-threads are executed in parallel, and the join function blocks the main process, so the child threads do not continue to execute the main thread until they have finished executing. You can use detach to detach a child thread from the main process, run it independently, and not block the main thread:
#include <iostream>#include<thread>#include<Windows.h>using namespacestd; voidthread01 () { for(inti =0; I <5; i++) {cout<<"Thread Working! "<<Endl; Sleep ( -); } } voidthread02 () { for(inti =0; I <5; i++) {cout<<"Thread is working! "<<Endl; Sleep ( $); } } intmain () {thread task01 (THREAD01); Thread task02 (THREAD02); Task01.detach (); Task02.detach (); for(inti =0; I <5; i++) {cout<<"Main thread is working! "<<Endl; Sleep ( $); } System ("Pause"); }
Output:
Executes in parallel using the main thread of detach and two sub-threads.
- Thread with parameter sub-thread
You can also pass parameters to a thread with parameters at the time of binding:
#include <iostream>#include<thread>#include<Windows.h>using namespacestd; //defining child threads with parametersvoidTHREAD01 (intnum) { for(inti =0; i < num; i++) {cout<<"Thread Working! "<<Endl; Sleep ( -); } } voidTHREAD02 (intnum) { for(inti =0; i < num; i++) {cout<<"Thread is working! "<<Endl; Sleep ( $); } } intmain () {thread task01 (THREAD01,5);//with parameter sub-threadThread Task02 (THREAD02,5); Task01.detach (); Task02.detach (); for(inti =0; I <5; i++) {cout<<"Main thread is working! "<<Endl; Sleep ( $); } System ("Pause"); }
Output:
- Multi-threaded synchronization Mutex
When multiple threads operate on the same variable at the same time, it is possible to cause the processing result to be abnormal if the variable does not have some protection processing:
#include <iostream>#include<thread>#include<Windows.h>using namespacestd; intTotalnum = -; voidthread01 () { while(Totalnum >0) {cout<< Totalnum <<Endl; Totalnum--; Sleep ( -); } } voidthread02 () { while(Totalnum >0) {cout<< Totalnum <<Endl; Totalnum--; Sleep ( -); } } intmain () {thread task01 (THREAD01); Thread task02 (THREAD02); Task01.detach (); Task02.detach (); System ("Pause"); }
Partial output results:
There are two problems, one is that a lot of variables are repeated output, and some of the variables are not output, and the second is normally the output of each thread after the data should be followed by a newline character, but most of this is the output of another thread.
This is because during the first thread-to-variable operation, the second thread also operates on the same variable, causing the output of the first thread to be processed as a result of the thread two operation. For this kind of data competition, you can use the thread mutex object mutex to keep the data synchronized. The use of the mutex class needs to include a header file mutex.
#include <iostream>#include<thread>#include<Windows.h>#include<mutex>using namespacestd; Mutex mu; //Thread Mutex Object intTotalnum = -; voidthread01 () { while(Totalnum >0) {mu.Lock();//Synchronizing Data Lockscout << Totalnum <<Endl; Totalnum--; Sleep ( -); Mu.unlock (); //Release Lock } } voidthread02 () { while(Totalnum >0) {mu.Lock(); cout<< Totalnum <<Endl; Totalnum--; Sleep ( -); Mu.unlock (); } } intmain () {thread task01 (THREAD01); Thread task02 (THREAD02); Task01.detach (); Task02.detach (); System ("Pause"); }
The output is normal after the mutex mutex object is added to the multi-thread:
C + + uses the thread class for multithreaded programming