您的位置:首页 > 其它

boost thread_group 的使用

2018-03-01 14:07 791 查看
boost库提供thread_group用于管理一组线程,就像是一个线程池,它内部使用std::list

#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 线程 线程池