boost thread_group 的使用
2018-03-01 14:07
791 查看
boost库提供thread_group用于管理一组线程,就像是一个线程池,它内部使用std::list
运行结果;
thread_group内部使用shared_mutex来保护线程list,所以它本身是线程安全的,在多线程环境里可以放心使用。
转自:http://blog.csdn.net/hanshuobest/article/details/53984660
#include <boost/thread/thread.hpp> #include <boost/bind.hpp> #include <boost/thread/mutex.hpp> #include <iostream> using namespace std ; boost::mutex iomutex ; void runchild(const int n) { { boost::mutex::scoped_lock lock(iomutex) ; cout<<"我是第"<<n<<"个子线程"<<endl ; } { boost::mutex::scoped_lock lock(iomutex) ; cout<<"进程"<<n<<" 退出 "<<endl ; } } void dummy(int n) { boost::mutex::scoped_lock lock(iomutex) ; for (int i = 0 ; i < n ; ++i) { cout<<i<<"\t" ; } cout<<endl ; } int main() { boost::thread_group group ; for (int num = 0 ; num < 10 ; ++num) { //create_thread()是一个工厂函数,可以创建thead对象并运行线程,同时加入内部的list // group.create_thread(boost::bind(&runchild , num)) ; group.create_thread(boost::bind(dummy , num)) ; } group.join_all() ; //等待所有线程执行结束 system("pause") ; return 1 ; }
运行结果;
thread_group内部使用shared_mutex来保护线程list,所以它本身是线程安全的,在多线程环境里可以放心使用。
转自:http://blog.csdn.net/hanshuobest/article/details/53984660
相关文章推荐
- boost::asio::io_service和io_service::work和boost::thread_group配合使用
- boost::asio::io_service和io_service::work和boost::thread_group配合使用
- 【boost】使用装饰者模式改造boost::thread_group
- boost_group线程池的使用
- 使用ThreadGroup模拟线程池
- boost thread 使用
- boost::thread使用引用参数
- Boost::Thread使用示例
- Boost::thread库的使用(转)
- boost综合使用<function,bind,thread,mutex,condition_variable,shared_ptr>
- BOOST 库 thread类线程使用 多线程处理的理解
- boost 中使用 线程池threadpool
- Boost笔记--Thread--Ubuntu上初次使用时遇到的问题
- Boost::thread库的使用
- boost:thread使用实例
- 一个使用boost::thread_specific_ptr线程本地存储的示例,value将不受其它线程影响
- Boost::Thread使用示例
- Jmeter使用jp@gc-stepping thread group做性能测试
- Boost::Thread使用示例 - CG-Animation - 博客频道 - CSDN.NET
- Boost.asio的简单使用(timer,thread,io_service类)