Boost - 多线程-boost recursive_mutex用法
来源:互联网 发布:ape播放软件 编辑:程序博客网 时间:2024/06/10 09:34
http://cooker.iteye.com/blog/748826
Author:QQ174554431
比较一下,就知道这个函数怎么用,效果如何.
- #include <iostream>
- void run()
- {
- for (int i = 0; i < 10; ++i)
- {
- std::cout << i << std::endl;
- }
- }
- int main(int argc, char* argv[])
- {
- boost::thread theard1(&run);
- boost::thread theard2(&run);
- boost::thread theard3(&run);
- theard1.join();
- theard2.join();
- theard3.join();
- return 0;
- }
结果:
0
1
2
3
00
11
22
37
48
59
6
37
4
8
5
9
6
7
8
9
杂乱无章的,一个线程执行输出时被其他线程干扰.
- #include <boost/thread/thread.hpp>
- #include <boost/thread/recursive_mutex.hpp>
- #include <iostream>
- boost::recursive_mutex io_mutex;
- void run()
- {
- for (int i = 0; i < 10; ++i)
- {
- boost::recursive_mutex::scoped_lock lock(io_mutex);
- std::cout << i << std::endl;
- }
- }
- int main(int argc, char* argv[])
- {
- boost::thread theard1(&run);
- boost::thread theard2(&run);
- boost::thread theard3(&run);
- theard1.join();
- theard2.join();
- theard3.join();
- return 0;
- }
结果:
0
1
2
3
4
5
6
7
8
9
0
1
0
2
1
3
4
5
6
2
7
3
4
8
5
9
6
7
8
9
输出时锁定, 就不会杂乱无章节.
- #include <boost/thread/thread.hpp>
- #include <boost/thread/recursive_mutex.hpp>
- #include <iostream>
- boost::recursive_mutex io_mutex;
- void run()
- {
- boost::recursive_mutex::scoped_lock lock(io_mutex);
- for (int i = 0; i < 10; ++i)
- {
- std::cout << i << std::endl;
- }
- }
- int main(int argc, char* argv[])
- {
- boost::thread theard1(&run);
- boost::thread theard2(&run);
- boost::thread theard3(&run);
- theard1.join();
- theard2.join();
- theard3.join();
- return 0;
- }
结果:
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
当一个线程被Lock,其他线程只能等待.
- Boost - 多线程-boost recursive_mutex用法
- Boost - 多线程-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 vs boost::recursive_mutex
- boost::recursive_mutex::scoped_lock
- boost递归锁recursive_mutex 测试
- 【Boost】boost库中thread多线程详解4——谈谈recursive_mutex(递归式互斥量)
- 【Boost】boost库中thread多线程详解4——谈谈recursive_mutex(递归式互斥量)
- 【Boost】boost库中thread多线程详解4——谈谈recursive_mutex
- 【Boost】boost库中thread多线程详解4——谈谈recursive_mutex(递归式互斥量)
- boost多线程简单用法
- 【原创】boost::recursive_mutex请小心使用
- boost 多线程
- boost多线程
- Boost多线程
- Tomcat7.0新特性:支持Servlet3.0注解定义,无需配置web.xml
- 七牛云存储qrsync命令行同步工具
- 同一点图层内最近点的查询与距离计算
- AT&T汇编格式
- 分享一个游戏源码第 BN赛艇
- Boost - 多线程-boost recursive_mutex用法
- POJ 1144 Network(割点数量)
- Row index must be greater than or equal to zero and less than the number of rows.
- tab control
- 例题7.1.3,分数拆分
- JAVA 连接常见数据库的方式总结
- Android获取已安装应用信息(图标,名称,版本号,包)
- RTP/RTCP包的识别以及流的识别
- 题目1497:面积最大的全1子矩阵