(Boost)mutex scoped_lock
来源:互联网 发布:淘宝代发论文被骗 编辑:程序博客网 时间:2024/05/26 09:54
原文:http://www.cnblogs.com/liuweilinlin/p/3255846.html
1.boost里的互斥量类型由mutex表示。
代码示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <vector>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
using
namespace
std;
using
namespace
boost;
int
main()
{
mutex mu;
try
{
this_thread::sleep(posix_time::seconds(2));
mu.lock();
//锁定cout对象
cout <<
"Some operations"
<<endl;
mu.unlock();
}
catch
(
int
)
{
mu.unlock();
return
0;
}
}
2.上面的代码好像似曾相识,是的,在防止内存泄露的时候采用的和上面类似的处理方式,更加简洁的方式是智能指针,类似的我们需要用智能锁改写上面的代码scoped_lock智能锁。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <vector>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
using
namespace
std;
using
namespace
boost;
template
<
typename
T>
class
basic_atom:noncopyable
{
private
:
T n;
typedef
mutex mutex_t;
mutex_t mu;
public
:
basic_atom(T x = T()):n(x){}
T operator++()
{
mutex_t::scoped_lock lock(mu);
return
++n;
}
operator T(){
return
n;}
};
int
main()
{
return
0;
}
0 0
- (Boost)mutex scoped_lock
- boost::mutex::scoped_lock
- boost: Mutex、scoped_lock 使用方式
- boost之Interprocess库,进程间通讯库之condition/mutex/scoped_lock
- boost的scoped_lock
- boost::recursive_mutex::scoped_lock
- boost多线程之scoped_lock理解
- boost mutex
- boost 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 mutex condition
- boost::thread mutex
- boost mutex lock 使用
- Boost Thread 临界区 mutex
- 关于IIS部署.net项目问题解决方法
- iOS app 更多操作 代码(邮件、facebook、twitter等)
- 邮箱几乎
- angularjs介绍
- Gcc常用命令详解
- (Boost)mutex scoped_lock
- spring mvc restful工程demo
- ros actionlib多客户端加单服务器的同步问题
- Swift语言精要 - Operator(运算符重载)
- Java中 复制文件并命名的超简洁写法
- hdu3440 House Man--单源最短路径&差分约束
- Java经典设计模式(2):七大结构型模式(附实例和详解)
- leetcode:Minimum Size Subarray Sum
- 易语言和VBS发送http请求 自动预约MZD记念馆团体票 分析第2步自动登陆预约carles抓包