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