c++ thread pool 使用解析

c++ thread pool相关总结

boost::threadpool

按照boost标准开发的第三方库。下载地址在http://threadpool.sourceforge.net/。使用方法较为简单。例子如下

#include <iostream>
#include "boost/bind.hpp"
#include "boost/threapool.hpp"
using namespace std;
using namespace threadpool;
void first_task()
{
    cout << "first task" << endl;
}
void second_task()
{
    cout << "second task" << endl;
}
void task_with_parameter(int value,string str)
{
    cout << "task with parameter,value is: " << value <<",str is: " << str;
}
int main()
{
    // 声明线程池
    pool thread_pool(2);
    // 向线程池中添加任务
    thread_pool.schedule(&first_task);
    // 等待线程函数执行完成
    thread_pool.wait();
    thread_pool.schedule(&second_task);
    thread_pool.schedule(boost::bind(task_with_parameter,8,"hello"));
    thread_pool.wait();
    return 0;
}

boost::threadpool 添加任务,同步方式都相对简单,在添加多参数的任务时候需要注意 boost::bind() 传递的参数是按照拷贝的方式传递的。如果想使用引用的方式传递的话,

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值