C ++ multi-threaded base study notes (five)

A mutex

1.1 The basic concept of a mutex

Simply put, a lock is a mutex, since it is locked, there are two states: locked and unlocked, lock operation >>> >>> share data by unlocking the way to achieve protection of shared data.

1.2 usage mutex

Role: After a piece of code to the lock, if need other other threads, etc. with the sections of the code executed, then unlock to proceed.

Header file: #include <mutex>

Member function: lock () // lock, unlock () // Unlock

Precautions: Use lock () and unlock () must exist in pairs.

 1 #include <iostream>
 2 #include <mutex>
 3 using namespace std;
 4 int num = 0;
 5 mutex my_mutex;         //创建一个互斥量
 6 void print1()
 7 {
 8     for (int i = 0; i < 50; i++)
 9     {
10         my_mutex.lock();
11         cout << "thread_1:" << num++ << endl;
12         my_mutex.unlock();
13     }
14 }
15 void print2()
16 {
17     for (int i = 0; i < 50; i++)
18     {
19         my_mutex.lock();
20         cout << "thread_2:" << num++ << endl;
21         my_mutex.unlock();
22     }
23 }
24 
25 int main()
26 {
27     thread thread_1(print1);      //打印0-49
28     thread thread_2(print2);      //打印49-99
29     thread_1.join();
30     thread_2.join();
31     system("pause");
32     return 0;
33 }

Another method, locking and unlocking can be implemented in two steps, i.e. using std :: lock_guard <mutex> myguard (my_mutex) instead lock () and UNLOCK (), the principle is, in the constructor std :: lock_guard performing the lock (), is performed in unlock destructor ().

 1 #include <iostream>
 2 #include <mutex>
 3 using namespace std;
 4 int num = 0;
 5 mutex my_mutex;
 6 void print1()
 7 {
 8     for (int i = 0; i < 50; i++)
 9     {
10         lock_guard<mutex> myguard(my_mutex);
11         cout << "thread_1:" << num++ << endl;
12     }
13 }
14 void print2()
15 {
16     for (int i = 0; i < 50; i++)
17     {
18         lock_guard<mutex> myguard(my_mutex);
19         cout << "thread_2:" << num++ << endl;
20     }
21 }
22 
23 int main()
24 {
25     thread thread_1(print1);      //打印0-49
26     thread thread_2(print2);      //打印49-99
27     thread_1.join();
28     thread_2.join();
29     system("pause");
30     return 0;
31 }

Second, deadlock

Only in the presence of two phenomena appear more mutex deadlock. Popular terms, that is, I'm waiting for you to unlock Unlock me, you wait for me to unlock you unlock.

. 1 #include <the iostream>
 2 #include <the mutex>
 . 3  the using  namespace STD;
 . 4  int NUM = 0 ;
 . 5  the mutex my_mutex1;    
 . 6  the mutex my_mutex2;
 . 7  void PRINT1 ()
 . 8  {
 . 9      for ( int I = 0 ; I < 500 ; ++ I )
 10      {
 . 11          . my_mutex1 Lock ();
 12 is          / * here are usually the same piece of code is used to manipulate the shared data * / 
13 is          my_mutex2. Lock();
14         cout << "thread_1:" << num++ << endl;
15         my_mutex1.unlock();
16         my_mutex2.unlock();
17     }
18 }
19 void print2()
20 {
21     for (int i = 0; i < 500; i++)
22     {
23         my_mutex2.lock();
24         my_mutex1.lock();
25         cout << "thread_2:" << num++ << endl;
26         my_mutex1.unlock();
27         my_mutex2.unlock();
28     }
29 }
30 
31 int main()
32 {
33     thread thread_1(print1);      //打印0-49
34     thread thread_2(print2);      //打印49-99
35     thread_1.join();
36     thread_2.join();
37     system("pause");
38     return 0;
39 }

It was found that also run without any mistakes Ben collapse phenomenon, but has been stuck there, will not continue any longer.

The solution of the deadlock is actually very simple, as long as the order of the same mutex lock on it. Or using std :: lock (mutex1, mutex2), meaning that the mutex to both simultaneously locked.

 

Guess you like

Origin www.cnblogs.com/main404/p/11221104.html
Recommended