Multithreading technology in C + + 11
C++11 new standard introduced four header files to support multithreaded programming, they are <atomic>
,, <thread>
<mutex>
, <condition_variable>
and <future>
.
<atomic>
: Provides atomic manipulation functionality, which mainly declares two classes, std::atomic and Std::atomic_flag, and also declares a set of C-style atomic types and functions that are compatible with C atomic operations.
<thread>
: Threading model encapsulation, the header file primarily declares the Std::thread class, and the Std::this_thread namespace is also in the header file.
<mutex>
: Mutex encapsulation, which primarily declares classes related to mutexes (mutexes), including Std::mutex series classes, Std::lock_guard, Std::unique_lock, and other types and functions.
<condition_variable>
: A condition variable that primarily declares classes related to condition variables, including std::condition_variable and Std::condition_variable_any.
<future>
: Implements a mechanism for asynchronous access to data provided by a specified data provider. The header file mainly declares std::p romise, std::p ackage_task Two Provider classes, and Std::future and Std::shared_future two future classes, plus some related types and functions, The Std::async () function is declared in this header file.
Simple example:
#include <iostream>#include<thread>using namespacestd;voidthread_1 () {cout<<"Hello from Thread_1"<<Endl;}intMainintargcChar**argv) {thread T1 (thread_1); /** JOIN () is equivalent to calling two functions: WaitForSingleObject, CloseHandle, in fact, in VC12 .*/T1.join (); return 0;}
View CodePrecautions
If a thread calls to a function in a class, you must declare the function as a static function function
Because static member functions belong to the static global zone, threads can share this area, so they can call each
#include <iostream>#include<pthread.h>using namespacestd; #defineNum_threads 5classHello { Public: //multi-threaded invocation, declared as static Static void* Say_hello (void*args) {cout<<"Hello ..."<<Endl; } }; intMain () {pthread_t tids[num_threads]; for(inti =0; i < num_threads; ++i) {intret = Pthread_create (&Tids[i], NULL, Hello::say_hello, NULL); if(Ret! =0) {cout<<"pthread_create Error:error_code"<< ret <<Endl; }} pthread_exit (NULL); }
View Code
Test results:
Hello ... Hello ... Hello ... Hello ...
View Code
If there is no main thread in the code, it will end up so that the pthread_join
entire process ends, so that the created thread does not have the opportunity to start executing. pthread_join
after joining, the main thread waits until the waiting thread ends itself, so that the created thread has an opportunity to execute.
Setting of attribute parameters when threading is created pthread_attr_t and use of join function
The properties of a thread are managed by the struct pthread_attr_t.
typedefstruct{ intDetachstate;//separation State of Threads intSchedpolicy;//Thread scheduling Policy structSched_param Schedparam;//scheduling parameters for threads intinheritsched;//the inheritance of threads intScope//Scope of the threadsize_t Guardsize;//Alert buffer size at the end of the thread stack intStackaddr_set; void* STACKADDR;//the location of the line stackssize_t stacksize;//the size of the line stacks}pthread_attr_t;
Example:
#include <iostream>#include<pthread.h>using namespacestd; #defineNum_threads 5void* Say_hello (void*args) {cout<<"Hello in thread"<< * ((int*) args) <<Endl; intStatus =Ten+ *((int*) args);//Add exit information when the thread exits, status for the main program to extract the end information of the threadPthread_exit ((void*) status); } intMain () {pthread_t tids[num_threads]; intIndexes[num_threads]; pthread_attr_t attr; //thread attribute struct, parameter added when creating threadPthread_attr_init (&ATTR);//InitializePthread_attr_setdetachstate (&attr, pthread_create_joinable);//is the setting you want to specify the thread attribute parameter, this parameter indicates that the thread can join the connection, the Join function means that the main program can wait until the end of the thread to do something, the main program and thread synchronization function for(inti =0; i < num_threads; ++i) {indexes[i]=i; intret = Pthread_create (&tids[i], &attr, Say_hello, (void*) &(Indexes[i])); if(Ret! =0) {cout<<"pthread_create error:error_code="<< ret <<Endl; }} Pthread_attr_destroy (&ATTR);//Freeing Memory void*status; for(inti =0; i < num_threads; ++i) {intret = Pthread_join (tids[i], &status);//The exit information status for each thread is obtained after the main program joins each thread if(Ret! =0) {cout<<"Pthread_join error:error_code="<< ret <<Endl; } Else{cout<<"pthread_join Get Status:"<< (Long) Status <<Endl; } } }
View Code
Test results:
HelloinchThread HelloinchThread 1helloinchThread3HelloinchThread40HelloinchThread2Pthread_joinGetStatusTenPthread_joinGetStatus OnePthread_joinGetStatus APthread_joinGetStatus -Pthread_joinGetStatus -
View Code
C++11 Multithreading