boost之thread
1.boost里的thread创建之后会立即启动。
代码示例:
#include <iostream>
#include <string>
#include <vector>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
using namespace std;
using namespace boost; mutex io_mu;
void printing(int& x,const string str)
{
for (int i = 0;i < 5;++i)
{
mutex::scoped_lock lock(io_mu);
cout << str << ++x <<endl;
}
} int main()
{
int x = 0;
thread(printing,x,"hello");
thread(printing,x,"boost");
this_thread::sleep(posix_time::seconds(2));
return 0; }
2.主线程等待和线程分离,为了防止主线程在子线程未执行完时就退出,可以使用posix_time::seconds和timed_join以及join
#include <iostream>
#include <string>
#include <vector>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
using namespace std;
using namespace boost; mutex io_mu;
void printing(int& x,const string str)
{
for (int i = 0;i < 5;++i)
{
mutex::scoped_lock lock(io_mu);
cout << str << ++x <<endl;
}
} int main()
{
int x = 0;
thread t1(printing,x,"hello");
thread t2(printing,x,"boost");
//this_thread::sleep(posix_time::seconds(2));
t1.timed_join(posix_time::seconds(1));
t2.join();
t1.detach();
return 0; }
3.操作线程,获取线程id和获得可并行(非并发)执行的线程数量。
#include <iostream>
#include <string>
#include <vector>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
using namespace std;
using namespace boost; mutex io_mu;
void printing(int& x,const string str)
{
for (int i = 0;i < 5;++i)
{
mutex::scoped_lock lock(io_mu);
cout << str << ++x <<endl;
}
} int main()
{
int x = 0;
thread t1(printing,x,"hello");
thread t2(printing,x,"boost"); cout << t1.get_id()<<endl;
cout << thread::hardware_concurrency()<<endl;
this_thread::sleep(posix_time::seconds(2)); return 0; }
4.线程中断
代码示例:
#include <iostream>
#include <string>
#include <vector>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
using namespace std;
using namespace boost; mutex io_mu;
void to_interrupt(int& x,const string str)
try
{
for (int i = 0;i < 5;++i)
{
this_thread::sleep(posix_time::seconds(1));
mutex::scoped_lock lock(io_mu);
cout << str << ++x <<endl;
}
}
catch(thread_interrupted&)
{
cout << "thread_interrupted" <<endl;
} int main()
{
int x = 0;
thread t(to_interrupt,x,"hello boost"); this_thread::sleep(posix_time::seconds(4));
t.interrupt();
t.join(); return 0; }
5.线程组
线程组可以看做是线程池,内部是使用顺序容器list<thread*>存放tread的指针。
#include <iostream>
#include <string>
#include <vector>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
using namespace std;
using namespace boost; mutex io_mu;
void printing(int& x,const string str)
{
for (int i = 0;i < 5;++i)
{
mutex::scoped_lock lock(io_mu);
cout << str << ++x <<endl;
}
} int main()
{
int x = 0;
thread_group tg;
tg.create_thread(bind(printing,x,"C++"));
tg.create_thread(bind(printing,x,"BOOST"));
tg.join_all();
return 0; }
boost之thread的更多相关文章
- boost:thread使用实例
/************************************************************************/ /*功能描述: boost thread使用实例 ...
- boost库thread.hpp编译警告honored已修复
请浏览:https://svn.boost.org/trac/boost/ticket/7874 #7874: compile warning: thread.hpp:342: warning: ty ...
- 关于boost的thread的mutex与lock的问题
妈的,看了好久的相关的知识,感觉终于自己有点明白了,我一定要记下来啊,相关的知识呀.... 1, 也可以看一下boost的线程指南:http://wenku.baidu.com/link?url=E_ ...
- 【boost】MFC dll中使用boost thread的问题
项目需要,在MFC dll中使用了boost thread(<boost/thread.hpp>),LoadLibraryEx的时候出现断言错误,去掉thread库引用后断言消失. 百度g ...
- Boost Thread学习笔记二
除了thread,boost种:boost::mutexboost::try_mutexboost::timed_mutexboost::recursive_mutexboost::recursive ...
- boost thread 在非正常退出时 内存泄露问题
在使用boost的thread库的时候,如果主程序退出,thread创建的线程不做任何处理,则会出现内存泄露. 解决方法: 在主线程退出时,对所有thread使用interrupt()命令,然后主程序 ...
- gcc boost版本冲突解决日记
问题背景 项目在Ubuntu10 64位 boost 1.55,boost采用的是项目内包含相对目录的形式部署 项目采用了 -Wall -Wextra -Werror -Wconversion 最高的 ...
- boost库(条件变量)
1相关理念 (1)类名 条件变量和互斥变量都是boost库中被封装的类. (2)条件变量 条件变量是thread库提供的一种等待线程同步的机制,可实现线程间的通信,它必须与互斥量配合使用,等待另一个线 ...
- Using Boost Libraries in Windows Store and Phone Applications
Using Boost Libraries in Windows Store and Phone Applications RATE THIS Steven Gates 18 Jul 2014 5:3 ...
随机推荐
- sql 基本操作
SQL基本操作 一数据类型1整数型 int2精确数值型 decimal(n,p)n为总位数,p为小数位数3浮点型 float4字符型char(n)n最大为4,varchar(n)5日期型datat ...
- decode行转列,case when,
1.行转列 转之前:
- centos内核优化--转至网络
A.关闭selinux(可以预防nginx修改根目录后访问出现404或者403) vi /etc/selinux/config SELINUX=disabled B.修改打开最大文件数句柄也就是 so ...
- 关于在javascript之中的时间格式;
如何获取当前日期: function CurentTime() { var now = new Date(); var year = now.getFullYear(); //年 var month ...
- 一款jQuery满屏自适应焦点图切换特效
一款jQuery满屏自适应焦点图切换特效 ,自适应当前浏览器的宽度,可以作为网站整个大背景的却换效果,很不错的一款不jquery特效. 兼容性没的说直接秒杀了IE6.适用浏览器:IE6.IE7.IE8 ...
- 2)main函数在执行前和执行后有哪些操作
main函数执行之前,主要就是初始化系统相关资源: 1. 设置栈指针 2. 初始化static静态和global全局变量,即data段的内容 3. 将未初始化部分的全局变 ...
- 使用WMIC永久设置你的环境变量
关于wmic,引用一下这位哥们儿的话http://technet.microsoft.com/en-us/library/bb742610.aspx: WMIC扩展WMI(Windows Manage ...
- 个人代码管理--svn
通常开发中遇到自己电脑和公司电脑代码共享的问题.比如一些通用的库,图片等项目中基本通用. 一些项目库如google code, github内地访问又挺困难的,常常无法连接,或者慢死..还有就是必须开 ...
- C++ 的全局构造与析构函数
我们知道一般的C/C++ 的程序是从main函数开始的,然后在main函数结束后程序结束.但是不然,在main函数开始执行前,已经有其他的指令被执行了. 为了程序的顺利执行,首先要初始化执行环境,比如 ...
- sql server 小记——分区表
我们知道很多事情都存在一个分治的思想,同样的道理我们也可以用到数据表上,当一个表很大很大的时候,我们就会想到将表拆 分成很多小表,查询的时候就到各个小表去查,最后进行汇总返回给调用方来加速我们的查询速 ...