C++中的unique_lock函数及其参数案例详解
2020-01-14 01:33
621 查看
#include <iostream> using namespace std; #include <list> #include <thread> #include <mutex> class A { public: std::unique_lock<std::mutex> rtn_unique_lock() { unique_lock<std::mutex> tmpguard(my_mutex); return tmpguard; } void receiveMes() { for (int i = 0; i < 10000; ++i) { //std::lock_guard<std::mutex> sbguard(my_mutex, std::adopt_lock); //std::unique_lock<std::mutex> sbguard(my_mutex, std::adopt_lock); //上述两种方法相似,默认在尾部析构函数中释放mutex std::unique_lock<std::mutex> sbguard (my_mutex, std::try_to_lock); //尝试去锁定mutex,不会卡在这里不动 //std::unique_lock<std::mutex> sbguard(my_mutex, std::defer_lock); //defer_lock是初始化一个没有加锁的mutex,其中的几个关键成员函数: lock(), unlock(), try_lock(),release(); //lock() 加锁 //unlock() 释放锁 //try_lock() 尝试加锁,如果成功则返回true,否则false //release() 返回它所管理的mutex对象指针,释放所有权,也就是 说这个unique_lock和mutex没有关系 mutex *Pmutex = sbguard.release() //锁住的代码的多少称为粒度 //sbguard 和 mutex之间的关系只能是一对一的关系,需要使用move函数,转移所有权 //std::unique_lock<std::mutex> sbguard1 (std::move(sbguard)); //或者:见上面的rtn_unique_lock()函数,返回一个局部变量会导致系统生成一个临时unique_lock对象,并调用unique_lock的移动构造函数 if (sbguard.owns_lock()) { msgQueue.push_back(i); cout << "msgQueue insert " << i << endl; } else { cout << "do some other thing" << endl; } } } void dealMsg() { for (int i = 0; i < 10000; ++i) { my_mutex.lock(); std::chrono::milliseconds dura(20000); this_thread::sleep_for(dura); if (!msgQueue.empty()) { int command = msgQueue.front(); cout << "deal msgQueue" << command << endl; msgQueue.pop_front(); } my_mutex.unlock(); } } private: list<int> msgQueue; mutex my_mutex; //互斥量 }; int main() { A myobj; thread threadReceive(&A::receiveMes, &myobj); //传入的对象要进行数据处理,所以传入引用。 thread threadDeal(&A::dealMsg, &myobj); threadReceive.join(); threadDeal.join(); return 0; }
转载于:https://www.cnblogs.com/qkqBeer/articles/10880670.html
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- C/C++中函数参数传递详解
- MySQL自定义函数用法详解-复合结构自定义变量/流程控制--及其相关案例
- C/C++中函数参数传递详解(从内存的分析)
- c/c++不定参数函数详解
- C/C++中函数参数传递详解
- C/C++中函数参数传递详解
- C/C++中函数参数传递详解
- C++中函数调用时的三种参数传递方式详解
- C/C++中函数参数传递详解
- 详解Python函数可变参数定义及其参数传递方式
- C/C++中函数参数传递详解
- C可变参数详解及其函数设计
- [C/C++] const 详解(修饰变量、输入参数、返回值、成员函数)
- C/C++中函数参数传递详解
- C++ unique函数用法详解
- C/C++中函数参数传递详解
- C/C++中函数参数传递详解
- C/C++函数参数传递机制详解及实例
- [C/C++] const 详解(修饰变量、输入参数、返回值、成员函数)
- C/C++中函数参数传递详解