c++多线程面试必备之 - 如何保证线程的执行顺序
1.互斥量(mutex)
用代码把共享数据锁住,其他尝试操作共享数据的线程必须等待 锁定->操作->解锁
头文件#include<mutex>
lock() unlock()是mutex的成员函数,成对使用,有lock必然要有unlock
using namespace std; class A { public: //收到数据,塞进容器中 void inMsgRecList() { for (int i = 0;i < 100000;i++) { cout << "inMsgRecList插入一个元素" << i << endl; my_mutex.lock(); msgRecList.push_back(i); my_mutex.unlock(); } } //从容其中取出数据 void outMsgRecList() { for (int i = 0;i < 100000;i++) { my_mutex.lock(); if (!msgRecList.empty()) { int msg = msgRecList.front(); msgRecList.pop_front(); my_mutex.unlock(); } else { cout << "outMsgRecList执行,但List中为空" << endl; my_mutex.unlock(); } } cout << "执行完了" << endl; } private: list<int> msgRecList; mutex my_mutex; }; int main() { A myobj; thread myOutthread(&A::outMsgRecList,&myobj); thread myInthread(&A::inMsgRecList, &myobj); myOutthread.join(); myInthread.join(); return 0; }
加了锁程序可以稳定运行不会再崩溃了,要记住if语句中每个分支都要unlock
unlock非常难排查所以引入了std::lock_guard类模板
lock_guard可以直接取代lock和unlock,使用lock_guard后不能使用lock和unlock了
mutex my_mutex; lock_gurad<mutex> myLockGud(my_mutex);
void inMsgRecList() { for (int i = 0;i < 100000;i++) { cout << "inMsgRecList插入一个元素" << i << endl; lock_guard<mutex> myLockGuard(my_mutex); msgRecList.push_back(i); } } //从容其中取出数据 void outMsgRecList() { for (int i = 0;i < 100000;i++) { lock_guard<mutex> myLockGuard(my_mutex); if (!msgRecList.empty()) { int msg = msgRecList.front(); msgRecList.pop_front(); } else { cout << "outMsgRecList执行,但List中为空" << endl; } } cout << "执行完了" << endl; }
lock_guard创建的时候调用构造函数lock,在函数执行结束前调用析构函数unlock,可以加上作用域使lock_guard提前析构
void inMsgRecList() { for (int i = 0;i < 100000;i++) { cout << "inMsgRecList插入一个元素" << i << endl; { lock_guard<mutex> myLockGuard(my_mutex); msgRecList.push_back(i); } } } //从容其中取出数据 void outMsgRecList() { for (int i = 0;i < 100000;i++) { lock_guard<mutex> myLockGuard(my_mutex); if (!msgRecList.empty()) { int msg = msgRecList.front(); msgRecList.pop_front(); } else { cout << "outMsgRecList执行,但List中为空" << endl; } } cout << "执行完了" << endl; }
死锁
至少两个锁头(互斥量)才能产生死锁问题
using namespace std; class A { public: //收到数据,塞进容器中 void inMsgRecList() { for (int i = 0;i < 100000;i++) { cout << "inMsgRecList插入一个元素" << i << endl; my_mutex1.lock(); my_mutex2.lock(); msgRecList.push_back(i); my_mutex1.unlock(); my_mutex2.unlock(); } } //从容其中取出数据 void outMsgRecList() { for (int i = 0;i < 100000;i++) { my_mutex2.lock(); my_mutex1.lock(); if (!msgRecList.empty()) { int msg = msgRecList.front(); msgRecList.pop_front(); my_mutex1.unlock(); my_mutex2.unlock(); } else { cout << "outMsgRecList执行,但List中为空" << endl; my_mutex1.unlock(); my_mutex2.unlock(); } } cout << "执行完了" << endl; } private: list<int> msgRecList; mutex my_mutex1; mutex my_mutex2; }; int main() { A myobj; thread myOutthread(&A::outMsgRecList,&myobj); thread myInthread(&A::inMsgRecList, &myobj); myOutthread.join(); myInthread.join(); return 0; }
解决死锁的办法
1.只要保证两个锁头的上锁顺序一致。
2.std::lock(mutex1,mutex2),
3.std::lock_guard<mutex> mylocgad(mutex1,std::adopt_lock);这个函数可以让lock_guard不执行构造函数,只执行析构函数,意思是可以手动lock,让lock_guard来帮助自动unlock,在lock的时候更加灵活了,在unlock的时候也更加方便了
class A { public: //收到数据,塞进容器中 void inMsgRecList() { for (int i = 0;i < 100000;i++) { cout << "inMsgRecList插入一个元素" << i << endl; std::lock(my_mutex1, my_mutex2); lock_guard<mutex> mylockguard1(my_mutex1,std::adopt_lock); lock_guard<mutex> mylockguard2(my_mutex2,std::adopt_lock); msgRecList.push_back(i); } } //从容其中取出数据 void outMsgRecList() { for (int i = 0;i < 100000;i++) { std::lock(my_mutex1, my_mutex2); lock_guard<mutex> mylockguard1(my_mutex1, std::adopt_lock); lock_guard<mutex> mylockguard2(my_mutex2, std::adopt_lock); if (!msgRecList.empty()) { int msg = msgRecList.front(); msgRecList.pop_front(); } else { cout << "outMsgRecList执行,但List中为空" << endl; } } cout << "执行完了" << endl; } private: list<int> msgRecList; mutex my_mutex1; mutex my_mutex2; };
unique_lock
unique_lock比lock_guard灵活,但是效率第一点,内存占用高一点
unique_lock<mutex> my_unilock(my_mutex);
第一种使用方法与lock_guard没有区别
unique_lock第二个参数
std::adopt lock ,必须提前手动lock互斥量,不在此构造函数中lock
std::try_to_lock ,尝试去锁定互斥量,但如果没有锁定成功,会直接return不回阻塞。
unique_lock<mutex> myulock(my_mutex1,std::try_to_lock); if(myulock.owns.lock()){ //拿到了锁 //可以操作全局数据 }else{ //妹拿到锁,不能操作全局数据,但可以做点别的。 }
std::defer_lock
unique_lock<mutex> myulock(my_mutex1,std::try_to_lock); //创建了一个my_mutex1的互斥量与myulock绑定,没有锁定,需要手动锁定 myulock.lock();#C++##面试#