标签:线程池 boost threadpool
前言:
#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();
}标签:线程池 boost threadpool
原文地址:http://blog.csdn.net/qingzai_/article/details/44488223