加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C++ Boost thread_group 编程详解

(2011-10-16 16:20:02)
标签:

thread_group

boost

c

it

分类: boost
1:thread_group是线程组的意思,可以实现对多个线程统一管理

2:成员函数如下:
1 http://www.cppblog.com/Images/OutliningIndicators/None.gifBoost thread_group 编程详解" TITLE="C++ Boost thread_group 编程详解" /> thread *  create_thread( const  boost::function0 < void >& );  // 创建一个线程
2 http://www.cppblog.com/Images/OutliningIndicators/None.gifBoost thread_group 编程详解" TITLE="C++ Boost thread_group 编程详解" /> void  add_thread(thread * );  // 加入一个已存在的线程
3 http://www.cppblog.com/Images/OutliningIndicators/None.gifBoost thread_group 编程详解" TITLE="C++ Boost thread_group 编程详解" /> void  remove_thread(thread * );  // 移除一个线程
4 http://www.cppblog.com/Images/OutliningIndicators/None.gifBoost thread_group 编程详解" TITLE="C++ Boost thread_group 编程详解" /> void  join_all();  // 全部等待结束

3:读者可以运行下面这个小例子
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>

using namespace boost;
using namespace std;

mutex iomutex;

void runChild(const int n)
{
    {
        mutex::scoped_lock lock(iomutex);
        cout << "我是第" << n << "个子线程" << endl;
    }

    {
        mutex::scoped_lock lock(iomutex);
        cout << "进程" << n << "退出" << endl;
     }
}

int main(int argc, char** argv)
{
   thread_group group;
   for(int num=0;num<10;num++)
       group.create_thread(bind(&runChild,num));
   group.join_all();
}
4:运行结果如下:


5:mutex是互斥,bind是绑定函数和函数的参数,thread_group即线程组

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有