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; }

最新文章

  1. TouchSlide1.1,手机上的幻灯片
  2. 人工智能系统Google开源的TensorFlow官方文档中文版
  3. wdlinux 一键安装
  4. ABAP程序系统字段中英文详解
  5. 【spring 6】Spring和Hibernate的整合:编程式事务
  6. HTML 中的meta标签中的http-equiv与name属性使用介绍
  7. uvalive 4589 Asteroids
  8. codis集群和redis cluster的优劣对比
  9. MySQL DBA修炼秘籍
  10. Javascript基础(2)
  11. flash导入图片缩放后出现毛边、失真、锯齿、文字模糊不清晰的情况
  12. 简单制作 OS X Yosemite 10.10 正式版U盘USB启动安装盘方法教程 (全新安装 Mac 系统)
  13. angularjs上传图片和文件
  14. Java进阶(三十八)快速排序
  15. Idea 的两个快捷键不能用的解决过程
  16. Zuul之Filter详解
  17. 《SpringMVC从入门到放肆》六、SpringMVC开发Controller的方法总结
  18. 最大获利 HYSBZ - 1497 (最大权闭合图)
  19. gdb 脚本调试
  20. Sublime Text shift+ctrl 妙用

热门文章

  1. MongoDB工具介绍
  2. Yii-数据模型- rules类验证器方法详解
  3. php中的一些小细节(1)
  4. Android消息推送完美方案[转]
  5. C++primer 阅读点滴记录(一)
  6. 開賣!下集 -- ASP.NET 4.5 專題實務(II)-範例應用與 4.5新功能【VB/C# 雙語法】
  7. 基于.NET的WebService的实现和WCF的实现
  8. SQL语句的执行顺序
  9. AngularJs记录学习01
  10. rspec的一些常见用法