这里,只是记录自己的学习笔记。
1.多线程同步通信
1.1多线程状态
1.1.1线程状态说明:
- 初始化 ( Init ) :该线程正在创建。
- 就绪 ( Ready ):该线程在继续列表中,等待CPU调度。
- 运行 (Running):该线程正在运行。
- 阻塞 ( Blocked ):该线程被阻塞挂起。Blocked状态包括:pend(锁、事件、信号量等阻塞)、suspend(主动pend)、delay(延时阻塞)、pendtime(因为锁、事件、信号量时间等 超时等待)。
- 退出 ( Exit ) :该线程运行结束,等待父线程回收其控制块资源。
1.1.2 竞争状态(Race Condition)和临界区(Critical Section)
竞争状态(Race Condition)
- 多线程同时读写共享数据。
临界区(Critical Section)
- 读写共享数据的代码片段
避免竞争状态策略,对临界区进行保护,同时只能有一个线程进入临界区。
mutex 的 lock() 和 try_lock() 方法的使用。
1 #include <iostream> 2 #include <thread> 3 #include <string> 4 #include <mutex> 5 using namespace std; 6 static mutex mux; 7 8 void TestThread() { 9 10 //对于锁,尽可能晚的申请;尽可能早的释放。。。 11 12 //获取锁资源,如果没有则阻塞等待 13 mux.lock();//处于阻塞状态,cpu资源是释放的。。。 14 cout << "========================================" << endl; 15 cout << "test 001" << endl; 16 cout << "test 002" << endl; 17 cout << "test 003" << endl; 18 cout << "========================================" << endl; 19 mux.unlock(); 20 } 21 22 23 void TestThread2() { 24 25 for (;;) { 26 //注意,调用 try_lock 是有开销的,在try_lock之后,必须跟一个sleep_for等待若干时间(释放cpu资源),否则一直try_lock会把cpu资源耗尽。 27 if (!mux.try_lock()) { 28 cout << "....." <<flush<< endl; 29 this_thread::sleep_for(3000ms); 30 continue; 31 } 32 cout << "========================================" << endl; 33 cout << "test 001" << endl; 34 cout << "test 002" << endl; 35 cout << "test 003" << endl; 36 cout << "========================================" << endl; 37 mux.unlock(); 38 this_thread::sleep_for(1000ms); 39 } 40 } 41 42 43 44 int main() 45 { 46 //for (int i = 0; i < 10; i++) { 47 // thread th(TestThread); 48 // th.detach(); 49 //} 50 51 for (int i = 0; i < 10; i++) { 52 thread th(TestThread2); 53 th.detach(); 54 } 55 56 getchar(); 57 return 0; 58 }
互斥锁等待坑,线程抢占不到资源.
特别注意,unlock之后,不要再立即进入lock。预留一点时间出来。
1 #include <iostream> 2 #include <thread> 3 #include <string> 4 #include <mutex> 5 using namespace std; 6 7 //互斥锁等待坑,线程抢占不到资源 8 static mutex mux; 9 10 void ThreradMainMux(int i) { 11 // 12 for (;;) { 13 14 mux.lock(); 15 cout << i << "[in]" << endl; 16 this_thread::sleep_for(1000ms); 17 mux.unlock(); 18 19 //特别注意,unlock之后,不要再立即进入lock。预留一点时间出来。 20 this_thread::sleep_for(1ms); 21 } 22 } 23 24 int main() { 25 for (int i = 0; i < 3; i++) 26 { 27 thread th(ThreradMainMux,i); 28 th.detach(); 29 } 30 31 getchar(); 32 return 0; 33 }