English Original: C + + Threading:make your (multitasking) life easier
Reprint: Http://www.oschina.net/translate/cplusplus-11-threading-make-your-multitasking-life
Introduced
This article is intended to help experienced WIN32 programmers understand the differences and similarities between the C + + 11 line libraries and the synchronization objects and Win32 threads and synchronization objects.
In Win32, all synchronization object handles (HANDLE) are global handles. They can be shared and even replicated between processes. In C++11, all synchronization objects are stack objects, which means they must be "detached (detached)" (if "detached" is supported) so that they can be refactored by the stack frame. If a large number of objects should be separated and you do not, then they will not be able to achieve their own actions, and destroy your original plan. (Translator Note: In Pthread, Threads have joinable and unjoinable, threads with joinable end without emptying the stack space occupied by the thread, usually after pthrea_create creates a thread, Then call Pthread_join (a bit of waitforsingleobject meaning) to empty, and Unjoinable's properties will automatically empty the space when the thread ends.
All c++11 synchronization objects have a native_handle () member that returns a concrete implementation handle (in Win32, it is a handle)
Background knowledge
I am also a novice c++11 thread. You need to understand the Win32 synchronization knowledge yourself. This may not be a suitable tutorial for synchronization techniques, but rather a quick guide to the c++11 mechanism to help you with the plan you have specified.
Simple Achievement Perfect
A simple example: Start a thread and wait for it to end:
| 12345678 |
void foo ()    {    } void func ()    {    STD:: thread t (foo); //starts. Equal to CreateThread.    t.join (); //Equal to WaitForSingleObject to the thread handle.    |
Unlike Win32 threads, you can pass parameters here:
| 123456789 |
void foo(int x,int y) { // x = 4, y = 5. }void func() { std::thread t(foo,4,5); // Acceptable. t.join(); } |
Thus, passing the ' this ' pointer to Std::thread makes the member function a thread and becomes a very simple thing. If std::thread is refactored and you do not call join () , it will be terminated abnormally. To run a thread out of C + + encapsulation:
| 23456789 |
void foo ()    {    void func ()    { STD:: thread t (foo);        //has called the Detach method here, the C + + object is detached from the Win32 object, and if the join method is called at this point, the Std::system_error () is thrown. Code class= "CPP Spaces" > t.detach ();    } |
In addition to join (),Detach ( ) method, there are joinable (),get_id (),sleep_for (),sleep_ Until (). They are all self-explanatory and well understood.
Using mutexes (mutexes)
Std::mutex is very similar to the critical section of Win32 (Cirtical). Lock () like entercriticalsection,unlock like leavecriticalsection,try_ Lock is like tryentercriticalsection.
?
| 12345678910111213141516 |
std::mutex m;int j = 0;void foo() { m.lock(); // 进入临界区域 j++; m.unlock(); // 离开 }void func() { std::thread t1(foo); std::thread t2(foo); t1.join(); t2.join(); // j = 2;} |
As above, you must unlock (unlock) after lock a Std::mutex object. If you have locked it, you cannot lock it again. This is different from Win32, if you are already in the critical area (critical section), EnterCriticalSection again will not fail, but will add a count.
The previous mention cannot repeat lock on std::mutex . There is Std::recursive_mutex(who invented the name), and its behavior is similar to the critical area (critical section) and can repeat lock.
?
| 123456789 |
std::recursive_mutex m; void foo ()    {    m.lock ();    m.lock (); //now valid    j++;    m.unlock ();    m.unlock (); //don ' t forget!    } |
In addition, there are Std::timed_mutex, Std::recursive_timed_mutex, they offer try_lock_for/ Try_lock_ The until method allows you to wait for a lock until it expires, or to reach the defined time.
C++11 Threads: Making your multi-threaded tasks easier