【Boost】boost库中thread多线程详解2——mutex与lock
来源:互联网 发布:g1x mark ii 知乎 编辑:程序博客网 时间:2024/05/17 08:43
1. mutex对象类
mutex类主要有两种:独占式与共享式的互斥量。▲ 独占式互斥量:
mutex: 独占式的互斥量,是最简单最常用的一种互斥量类型
try_mutex: 它是mutex的同义词,为了与兼容以前的版本而提供
timed_mutex: 它也是独占式的互斥量,但提供超时锁定功能
▲ 递归式互斥量:
recursive_mutex: 递归式互斥量,可以多次锁定,相应地也要多次解锁
recursive_try_mutex: 它是recursive_mutex 的同义词,为了与兼容以前的版本而提供
recursive_timed_mutex: 它也是递归式互斥量,基本功能同recursive_mutex, 但提供超时锁定功能
▲ 共享式互斥量:
shared_mutex: multiple-reader/single-writer 型的共享互斥量(又称读写锁)。
其中mutex有lock和unlock方法,shared_mutex除了提供lock和unlock方法外,还有shared_lock和shared_unlock方法。
2. lock模板类
▲ 独占锁:boost::unique_lock<T>,其中T可以mutex中的任意一种。
如果T为mutex,那么boost::unique_lock<boost::mutex>,构造与析构时则分别自动调用lock和unlock方法。
如果T为shared_mutex,那么boost::unique_lock<boost::shared_mutex>,构造与析构时则分别调用shared_mutex的shared_lock和shared_unlock方法。
注意:scoped_lock也是独占锁,其源代码中定义如下;
typedef unique_lock<mutex> scoped_lock;
typedef unique_lock<timed_mutex> scoped_timed_lock;
▲ 共享锁:
boost::shared_lock<T>,其中的T只能是shared_mutex类。
当然还有其他一些锁:lock_guard, upgrade_lock等。
3. 读写锁的实现
- typedef boost::shared_lock<boost::shared_mutex> readLock;
- typedef boost::unique_lock<boost::shared_mutex> writeLock;
- boost::shared_mutex rwmutex;
- void readOnly()
- {
- readLock rdlock(rwmutex);
- // do something
- }
- void writeOnly()
- {
- writeLock wtlock(rwmutex);
- // do something
- }
4. boost::lock_guard<>和boost::unique_lock<>的区别
- boost::mutex m;
- void foo( )
- {
- boost::lock_guard<boost::mutex> lk(m);
- process(data);
- };
- // lock_guard只能像上面这样使用,而unique_lock允许设置超时,推迟锁定lock以及在对象销毁之前unlock。
- {
- boost::unique_lock<boost::mutex> lk( m );
- process( data );
- lk.unlock( );
- // do other thing
- };
- // 设置锁超时
- {
- boost::unique_lock<boost::timed_mutex> lk(m, std::chrono::milliseconds(3)); // 超时3秒
- if(lk)
- process( data );
- };
5. 简单示例
- namespace
- {
- boost::mutex mutex;
- boost::shared_mutex shared_mutex;
- void wait(int seconds)
- {
- boost::this_thread::sleep(boost::posix_time::seconds(seconds));
- }
- void threadfun1()
- {
- for (int i = 0; i < 5; ++i)
- {
- wait(1);
- mutex.lock();
- PRINT_DEBUG(i);
- mutex.unlock();
- }
- }
- void threadfun2()
- {
- for (int i = 0; i < 5; ++i)
- {
- wait(1);
- boost::lock_guard<boost::mutex> lock(mutex);
- PRINT_DEBUG(i);
- }
- }
- void threadfun3()
- {
- for (int i = 0; i < 5; ++i)
- {
- wait(1);
- // unique_lock<boost::mutex> = scoped_lock
- boost::unique_lock<boost::mutex> lock(mutex);
- std::cout << lock.owns_lock() << std::endl;
- PRINT_DEBUG(i);
- }
- }
- }
- // 1. mutex例子
- void test_thread_syn1()
- {
- boost::thread t1(&threadfun1);
- boost::thread t2(&threadfun1);
- t1.join();
- t2.join();
- }
- // 2. lock_guard例子
- void test_thread_syn2()
- {
- boost::thread t1(&threadfun2);
- boost::thread t2(&threadfun2);
- t1.join();
- t2.join();
- }
- // 3. scoped_lock例子
- void test_thread_syn3()
- {
- boost::thread t1(&threadfun3);
- boost::thread t2(&threadfun3);
- t1.join();
- t2.join();
- }
0 0
- 【Boost】boost库中thread多线程详解2——mutex与lock
- 【Boost】boost库中thread多线程详解2——mutex与lock
- boost库中thread多线程详解2——mutex与lock
- boost库中thread多线程详解——mutex与lock
- boost库中thread多线程详解——mutex与lock
- 【Boost】boost库中thread多线程详解1——thread入门与简介
- 【Boost】boost库中thread多线程详解7——wait与timed_wait简单例子
- 【Boost】boost库中thread多线程详解7——wait与timed_wait简单例子
- 【Boost】boost库中thread多线程详解12——线程的分离与非分离
- 【Boost】boost库中thread多线程详解3——细说lock_guard
- 【Boost】boost库中thread多线程详解4——谈谈recursive_mutex(递归式互斥量)
- 【Boost】boost库中thread多线程详解5——谈谈线程中断
- 【Boost】boost库中thread多线程详解6——线程组简单例子
- 【Boost】boost库中thread多线程详解4——谈谈recursive_mutex(递归式互斥量)
- 【Boost】boost库中thread多线程详解3——细说lock_guard
- 【Boost】boost库中thread多线程详解4——谈谈recursive_mutex
- 【Boost】boost库中thread多线程详解5——谈谈线程中断
- 【Boost】boost库中thread多线程详解6——线程组简单例子
- 一种动态开辟二维数组的办法
- JAVA单链表基本操作
- UIWebView中显示js代码写的提示框带域名问题
- 关于 回调 、 反射 和异步的一些理解---初级知识
- [Android] Webview利用CookieSyncManager获取或设置Cookies的策略
- 【Boost】boost库中thread多线程详解2——mutex与lock
- YTU 2811: 打鱼还是晒网
- QT5使用消息机制及程序发布相关
- Proxy-Style Redis集群设计
- Makefile中的-C和M=解析
- 【Boost】boost库中thread多线程详解3——细说lock_guard
- opendaylight实验远程调试
- oracle 以及 mysql 求经纬度之间距离的函数
- python读取剪切板内容