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 ...
随机推荐
- compareTo & toString
public class UnAssignedRecord implements Comparable<UnAssignedRecord> { private String time; / ...
- Mongod(5):启动命令mongod参数说明
Mongodb启动命令mongod参数说明(http://blog.csdn.net/fdipzone/article/details/7442162) mongod的主要参数有: 基本配置 ---- ...
- javascript变量和对象要注意的点
js对象和变量,对象本身是没有名称的,之所以使用变量是为了通过某个名称来称呼这样一种不具名称的对象.变量分基本变量和引用类型变量.将基本类型的的值付给变量的话,变量将这个值本身保存起来. <sc ...
- Nginx配置:http重定向,URLRewrite,一个简单框架的配置思路
一个重定向的应用配置: server { listen 8000; server_name localhost; root F:/home/projects/test; index ...
- CentOS7.0安装与配置Tomcat-7
解决权限不够 #chmod a+x filename 安装说明 安装环境:CentOS-7.0.1406安装方式:源码安装 软件:apache-tomcat-7.0.29.tar.gz 下载地址:ht ...
- 并行编程之CountdownEvent的用法
教程:http://blog.gkarch.com/threading/part5.html#the-parallel-class http://www.cnblogs.com/huangxinche ...
- 条件放在left join后面和where后面
有这样一个查询的差异: 两张表如下: 语句在这里: create table #AA ( ID int, Name nvarchar() ) insert into #AA ,'项目1' union ...
- 数据结构学习笔记05图(最小生成树 Prim Kruskal)
最小生成树Minimum Spanning Tree 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边. 树: 无回路 |V|个顶 ...
- [terry笔记]Oracle会话追踪(二):TKPROF
接上一笔记[terry笔记]Oracle会话追踪(一):SQL_TRACE&EVENT 10046 http://www.cnblogs.com/kkterry/p/3279282.html ...
- hdu 4027 Can you answer these queries?
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Description Proble ...