C ++ 11 multithreading learning ---- simple use of std: thread class
Introduction to one C ++ 11 Multithreading
The C ++ 11 Standard Library provides classesthread
(std::thread
). To run a thread, you can create a classthread
Object. Its initial parameters are a function object and the parameters required by the function object. Using member functionsstd::thread::join()
Support for thread Union. One thread can be paused until other threads have finished running. If the underlying platform supports this functionstd::thread::native_handle()
Provides platform-specific operations for native thread objects. For synchronization between threads, the standard library will provide appropriate mutex locks (suchstd::mutex
,std::recursive_mutex
And condition parameters (std::condition_variable
Andstd::condition_variable_any
). The aforementioned synchronization mechanism will use the RAII lock (std::lock_guard
Andstd::unique_lock
) And lock-related algorithms to facilitate the use of programmers.
For jobs that require high performance or extremely low level, sometimes or even necessary, we hope that inter-thread communication can avoid overhead on the use of mutex locks. This can be achieved by accessing the memory through atomic operations. For different situations, we can change the visibility of the Access Memory action through the explicit memory barrier.
For asynchronous transmission between threads, the C ++ 11 standard library is added andstd::packaged_task
It is used to encapsulate a function call that will return asynchronous results. The proposal of futures was criticized because it lacked the ability to combine multiple futures and could not determine whether a promise in a set of promise sets was completed.
More advanced thread support, such as the thread pool, has decided to add such support to future TechnicalReport. More advanced thread support is not part of C ++ 11, but it is assumed that its final implementation will be created on the existing thread support.
std::async
Provides a simple method to run a thread and bind the threadstd::future
. You can choose whether to run asynchronously on multiple threads or run on one thread and wait for the required data. By default, you can select one of the first two options based on the underlying hardware. In addition, the thread pool can also be used to provide support in simple use cases.
Two simple std: Use of thread
#include
#include
using namespace std; void myFirstThread(){ cout << "Hello thread" << endl;} int main(){ thread myThread(myFirstThread); myThread.join(); return 0;}
3. std: thread class Constructor
Std: thread Construction
1. Create an empty thread object by default. The following is the default constructor declaration:
Thread () notest;
2. copy-delete ):
Thread (const thread &) = delete;
3. initialize the constructor and create a thread object. This object can be joinable and the thread will call the fn function. The parameters are provided by args. The initialization constructor declaration is as follows:
Template
Explicit thread (Fn & fn, Args &... args );
4. move the constructor. After the constructor is called successfully, x does not represent any executable objects of the thread.
Thread (thread & x) notest;
Note: joinable thread objects must be joined by the main thread or set to detached before they are destroyed.
Examples of std: thread constructors are as follows:
# Include
# Include
Using namespace std; void myFirstThreadTask (int num) {for (int I = 0; I <10; ++ I) {cout <"myFirstThreadTask's num =" <num ++ <endl; this_thread: sleep_for (chrono: milliseconds (10 ));}} void mySecondThreadTask (int & num) {for (int I = 0; I <10; ++ I) {cout <"mySecondThreadTask's num =" <num ++ <endl; this_thread: sleep_for (std: chrono: milliseconds (10 ));}} int _ tmain (int argc, _ TCHAR * argv []) {int n = 5; thread myThread0; // myThread1 is an empty thread, it does not mean that any executable object // myThread1 is bound to the myFirstThreadTask function, and n is used to pass the thread myThread1 (myFirstThreadTask, n) by value; // myThread2 is bound to the mySecondThreadTask function, n is the reference parameter of thread myThread2 (mySecondThreadTask, std: ref (n); myThread1.join (); myThread2.join (); n ++; // currently, myThread2 does not represent any executable object. myThread3 and mySecondThreadTask function are bound to thread myThread3 (std: move (myThread2); // myThread3.join (); return 0 ;}