boost::threadpool线程池使用实例

时间:2015-03-20 10:53:53   收藏:0   阅读:872

前言:

示例:
#include <iostream>
#include <boost/bind.hpp>
#include <boost/threadpool.hpp>
using namespace std;
using namespace boost::threadpool;

// 下面是3个不带参数的线程执行函数和一个带参数的线程执行函数
void first_task()
{
   for (int i = 0; i < 30; ++i)
   cout << "first" << i << endl;
}
void second_task()
{
   for (int i = 0; i < 30; ++i)
   cout << "second" << i << endl;
}
void third_task()
{
   for (int i = 0; i < 30; ++i)
   cout << "third" << i << endl;
}
void task_with_parameter(int value, string str)
{
  printf("task_with_parameter with int=(%d).\n" ,value);
  printf("task_with_parameter with string=(%s).\n" ,str.c_str());
}
void execute_with_threadpool()
{
  // 创建一个线程池,初始化为2个线程
  pool tp(2);
 
  // 调用4个线程函数
  tp.schedule(&first_task); // 不带参数的执行函数
  tp.schedule(&second_task); 
  tp.schedule(&third_task); 
  tp.schedule(boost::bind(task_with_parameter, 8, "hello")); // 带两个参数的执行函数
  // 这儿函数会等到4个线程全部执行结束才会退出
}
int main(int argc, const char* argv[])
{
    execute_with_threadpool();
}


原文:http://blog.csdn.net/qingzai_/article/details/44488223

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!