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,其他线程只能等待.
分享到:
相关推荐
基于opencv实现Recursive_implementation_of_the_Gaussian_filter
用递归的方法把一个数倒序输出例:输入12345输出:54321
Introduction to recursive programming
Recursive_Wang_Tiles这篇文章的源码
递推辨识理论与应用,英文版 这本书提供了一个发展全面系统...这种方法,这也被认为是连续的自适应算法或参数估计的方法众所周知,可应用于在线,自适应系统广泛,如信号处理,预测或控制装置,并在建模通用系统有用。
Super-Recursive Algorithms
汉诺塔非递归程序,包含详细的解析、代码、结果及心得
二叉树的非递归先根遍历遍历二叉树的非递归先根遍历遍历二叉树的非递归先根遍历遍历
用法安装pip install dict-recursive-update例子 >> > from dict_recursive_update import recursive_update>> > recursive_update ({ 'a' : { 'b' : 2 }}, { 'a' : { 'b' : 3 , 'd' : 4 }, 'e' : 5 }){ 'a' : { 'b'...
如果计算出的有效波高与观测数据相比太大,则尝试使用更多的级联来耗散更多的能量。 如果将风插值用于频谱解析模型,请使用4-7级联生成完整的u10,然后在与模型时间步长相对应的窗口上求平均值。 例如,如果时间步长...
huffman encoder using recursive function
数据结构里的dfs算法,包括递归与非递归。用vc6.0编写
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
通过设计、编制、调试一个递归下降语法分析程序,实现对词法分析程序所提供的单词序列进行语法检查和结构分析,掌握常用的语法分析方法。通过本实验,应达到以下目标: 1、掌握从源程序文件中读取有效字符的方法和...
ACF到REST API递归 递归获取ACF字段 安装 ... 如何使用: add_filter ( 'acf/rest_api/recursive/types' , function ( $ types ) { if ( isset ( $ types [ 'post' ] ) ) { unset( $ types [ 'p
在图像插帧领域中对真实运动向量进行估计的算法,此算法较为经典,3DRS
VS2008xia MFC编程,计算机图形学画图程序:包括递归种子填充、简单种子填充、扫描线种子填充、图案填充
Matching of wildcards and recursive mirroring of directories are available when retrieving via FTP. Both HTTP and FTP retrievals can be time-stamped, thus Wget can see if the remote file has changed ...
recursive_ftp_download 使用FTP函数使用php下载文件递归原始资源可以在以下找到: : 作者:cukabeka脚本名称:recursive_ftp_download.php