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的更多相关文章

  1. boost:thread使用实例

    /************************************************************************/ /*功能描述: boost thread使用实例 ...

  2. boost库thread.hpp编译警告honored已修复

    请浏览:https://svn.boost.org/trac/boost/ticket/7874 #7874: compile warning: thread.hpp:342: warning: ty ...

  3. 关于boost的thread的mutex与lock的问题

    妈的,看了好久的相关的知识,感觉终于自己有点明白了,我一定要记下来啊,相关的知识呀.... 1, 也可以看一下boost的线程指南:http://wenku.baidu.com/link?url=E_ ...

  4. 【boost】MFC dll中使用boost thread的问题

    项目需要,在MFC dll中使用了boost thread(<boost/thread.hpp>),LoadLibraryEx的时候出现断言错误,去掉thread库引用后断言消失. 百度g ...

  5. Boost Thread学习笔记二

    除了thread,boost种:boost::mutexboost::try_mutexboost::timed_mutexboost::recursive_mutexboost::recursive ...

  6. boost thread 在非正常退出时 内存泄露问题

    在使用boost的thread库的时候,如果主程序退出,thread创建的线程不做任何处理,则会出现内存泄露. 解决方法: 在主线程退出时,对所有thread使用interrupt()命令,然后主程序 ...

  7. gcc boost版本冲突解决日记

    问题背景 项目在Ubuntu10 64位 boost 1.55,boost采用的是项目内包含相对目录的形式部署 项目采用了 -Wall -Wextra -Werror -Wconversion 最高的 ...

  8. boost库(条件变量)

    1相关理念 (1)类名 条件变量和互斥变量都是boost库中被封装的类. (2)条件变量 条件变量是thread库提供的一种等待线程同步的机制,可实现线程间的通信,它必须与互斥量配合使用,等待另一个线 ...

  9. 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 ...

随机推荐

  1. [译]Memory Reordering Caught in the Act

    原文:http://preshing.com/20120515/memory-reordering-caught-in-the-act/ 编写lock-free的C/C++程序时,在保证memory  ...

  2. 使用DBCP时发生AbstractMethodError异常

    使用DBCP时发生AbstractMethodError异常,错误描述: Exception in thread "main" java.lang.AbstractMethodEr ...

  3. php时间转换unix时间戳

    本文介绍了php编程中unix时间戳转换的小例子,有关php时间转换.php时间戳的实例代码,有需要的朋友参考下. 第一部分,php 时间转换unix 时间戳实现代码. 复制代码代码示例: <? ...

  4. [习题]日历(Calendar)控件的障眼法(.Visible属性),使用时才出现?不用就消失?

    原文出處  http://www.dotblogs.com.tw/mis2000lab/archive/2013/09/02/calendar_icon_visible.aspx [习题]日历(Cal ...

  5. Hadoop1.x与2.x安装笔记

    Hadoop1.x与2.x安装笔记 Email: chujiaqiang229@163.com 2015-05-09 Hadoop 1.x 安装 Hadoop1.x 集群规划 No 名称 内容 备注 ...

  6. POJ--1416

    #include <stdio.h> #include <stdlib.h> ]={};//代表有没有切割的数组 ;//输入的要被切割的数字 ]={};//切完输出的数组成的数 ...

  7. 【转】 BSS段 数据段 代码段 堆栈 指针 vs 引用

    原文:http://blog.csdn.net/godspirits/article/details/2953721 BSS段 数据段 代码段 堆栈 (转+) 声明:大部分来自于维基百科,自由的百科全 ...

  8. .NET String.Format 方法 线程安全问题

    碰到这个问题 是在和淘宝做信息交互的时候, 接收别人N年前的代码. 代码逻辑很简单,就是取得信息 数据库查询  响应请求返回结果. 最近淘宝的人反映说 N多账户使用的是一个单号.理论上来说 是应该每次 ...

  9. ORACLE-12C-RAC INSTALL

    OS: Oracle Linux Server release 5.7 DB: 12.1.0.1.0 挂载镜像:mkdir /media/diskmount /dev/cdrom /media/dis ...

  10. oracle 日志文件管理

    OS: [root@b28-122 ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 DB: SQL> select * f ...