boost::mutex vs boost::recursive_mutex
来源:互联网 发布:模拟人生4帅哥捏脸数据 编辑:程序博客网 时间:2024/05/19 03:18
boost::mutex is not re-entrant, a thread can only lock it once, otherwise it’s dead-locked. The following code snippet demonstrates it:
#include "boost/thread/mutex.hpp"
#include <iostream>
boost::mutex mtx;
void bar(){
boost::mutex::scoped_lock lLock(mtx);//!! dead-locked here
std::cout << "bar" << std::endl;
}
void foo() {
boost::mutex::scoped_lock lLock(mtx);
std::cout << "foo" << std::endl;
bar();
}
int _tmain(int argc, _TCHAR* argv[]){
foo();
return 0;
}
If you need re-entrant mutex, the boost::recursive_mutex is the choice. The following code snippet demonstrates it:
#include "boost/thread/recursive_mutex.hpp"
#include <iostream>
boost::recursive_mutex r_mtx;
void bar(){
boost::recursive_mutex::scoped_lock lLock(r_mtx);//no problem for a thread to lock more than once
std::cout << "bar" << std::endl;
}
void foo() {
boost::recursive_mutex::scoped_lock lLock(r_mtx);
std::cout << "foo" << std::endl;
bar();
}
int _tmain(int argc, _TCHAR* argv[]){
foo();
return 0;
}
I also did a benchmark on my PC. For 1 locking operation, the result is approximately: boost::mutex: 0.043 micro second, boost::recursive_mutex: 0.068 micro second.
Re-entrant mutex is the default in Java and C#. Generally speaking, if a mutex is shared by many modules/classes, it’s recommended to use boost::recursive_mutex; while if it’s only used by a single module/class and no re-entrant feature needed, it’s recommended to use boost::mutex.
Mutex可以分为递归锁(recursive mutex)和非递归锁(non-recursive mutex)。可递归锁也可称为可重入锁(reentrant mutex),
非递归锁又叫不可重入锁(non-reentrant mutex)。
二者唯一的区别是,同一个线程可以多次获取同一个递归锁,不会产生死锁。而如果一个线程多次获取同一个非递归锁,则会产生死锁。
boost::mutex is not re-entrant, a thread can only lock it once, otherwise it’s dead-locked. The following code snippet demonstrates it:
#include "boost/thread/mutex.hpp"
#include <iostream>
boost::mutex mtx;
void bar(){
boost::mutex::scoped_lock lLock(mtx);//!! dead-locked here
std::cout << "bar" << std::endl;
}
void foo() {
boost::mutex::scoped_lock lLock(mtx);
std::cout << "foo" << std::endl;
bar();
}
int _tmain(int argc, _TCHAR* argv[]){
foo();
return 0;
}
If you need re-entrant mutex, the boost::recursive_mutex is the choice. The following code snippet demonstrates it:
#include "boost/thread/recursive_mutex.hpp"
#include <iostream>
boost::recursive_mutex r_mtx;
void bar(){
boost::recursive_mutex::scoped_lock lLock(r_mtx);//no problem for a thread to lock more than once
std::cout << "bar" << std::endl;
}
void foo() {
boost::recursive_mutex::scoped_lock lLock(r_mtx);
std::cout << "foo" << std::endl;
bar();
}
int _tmain(int argc, _TCHAR* argv[]){
foo();
return 0;
}
I also did a benchmark on my PC. For 1 locking operation, the result is approximately: boost::mutex: 0.043 micro second, boost::recursive_mutex: 0.068 micro second.
Re-entrant mutex is the default in Java and C#. Generally speaking, if a mutex is shared by many modules/classes, it’s recommended to use boost::recursive_mutex; while if it’s only used by a single module/class and no re-entrant feature needed, it’s recommended to use boost::mutex.
Mutex可以分为递归锁(recursive mutex)和非递归锁(non-recursive mutex)。可递归锁也可称为可重入锁(reentrant mutex)二者唯一的区别是,同一个线程可以多次获取同一个递归锁,不会产生死锁。而如果一个线程多次获取同一个非递归锁,则会产生死锁。
- boost::mutex vs boost::recursive_mutex
- boost::mutex vs boost::recursive_mutex
- boost::mutex vs boost::recursive_mutex
- boost::mutex vs boost::recursive_mutex
- boost::mutex vs boost::recursive_mutex
- Boost - 多线程-boost recursive_mutex用法
- Boost - 多线程-boost recursive_mutex用法
- boost::recursive_mutex::scoped_lock
- boost mutex
- boost Mutex
- boost递归锁recursive_mutex 测试
- 【原创】boost::recursive_mutex请小心使用
- boost mutex condition
- boost::thread mutex
- (Boost)mutex scoped_lock
- boost::mutex::scoped_lock
- boost mutex lock 使用
- boost::unique_lock vs boost::lock_guard
- 11级_Java_曹建波6.05 索引
- Linux下getsockoptsetsockopt_函数说明.
- C语言中static的用法
- 6-5 长城维修测试
- 基于C#Winform的简易聊天程序客户端(3)
- boost::mutex vs boost::recursive_mutex
- SQL为字段添加一个唯一约束
- HRBEU/HEUOJ----1002 A Star not a Tree?
- VS2008 stack overflow
- 屏蔽siverlight的右键
- poj 1316 Self Numbers
- memcpy 和 memmove 的区别,memcpy 和 strcpy 的区别
- Excel导入数据库过程中遇到的问题
- 5_28_天天向上