boost创建线程池_使用boost实现线程池thread pool | boost thread pool example

本文通过示例展示了如何使用Boost库创建线程池,包括线程池的构造、销毁,以及如何向线程池中添加任务。还演示了使用future和promise进行异步操作的例子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

boost thread pool example

Guide

boost thread pool example with cpp code

code example

#include //std::cout std::endl

#include //std::thread

#include //std::future std::promise

#include //std::ref

#include //std::chrono::seconds

#include

#include

#include

class ThreadPool {

public:

explicit ThreadPool(size_t size) : work_(io_service_) {

for (size_t i = 0; i < size; ++i) {

workers_.create_thread(

boost::bind(&boost::asio::io_service::run, &io_service_));

}

}

~ThreadPool() {

std::cout << "~ThreadPool" << std::endl;

io_service_.stop(); // stop before join_all

workers_.join_all();

}

// Add new work item to the pool.

template

void Enqueue(F f) {

io_service_.post(f);

//sync, return immediately

}

private:

boost::thread_group workers_;

boost::asio::io_service io_service_;

boost::asio::io_service::work work_;

};

boost::mutex io_mutex;

void count(int id)

{

for (int i = 0; i < 10; i++)

{

boost::mutex::scoped_lock lock(io_mutex);

std::cout << id << ":" << i << std::endl;

}

}

void test_thread()

{

boost::thread thrd1(boost::bind(&count, 1));

boost::thread thrd2(boost::bind(&count, 2));

thrd1.join();

thrd2.join();

}

void print(int i)

{

boost::mutex::scoped_lock lock(io_mutex);

std::cout << "print() #" << boost::this_thread::get_id() << std::endl;

std::cout << "hello " << i << std::endl;

boost::this_thread::sleep(boost::posix_time::seconds(1));

std::cout << "world " << i << std::endl;

}

void test_thread_pool()

{

// Create a thread pool of 4 worker threads.

ThreadPool pool(4);

// Queue a bunch of work items.

for (int i = 0; i < 8; ++i) {

pool.Enqueue(boost::bind(&print, i));

}

}

void do_task(std::promise &promiseObj) {

boost::mutex::scoped_lock lock(io_mutex);

std::cout << "Inside thread: " << std::this_thread::get_id() << std::endl;

std::cout << "Inside thread: sleep 2 seconds... " << std::endl;

std::this_thread::sleep_for(std::chrono::seconds(2));

promiseObj.set_value(35);

}

void test_future()

{

std::promise promiseObj;

std::future futureObj = promiseObj.get_future();

std::cout << "create thread..." << std::endl;

std::thread th(do_task, std::ref(promiseObj));

std::cout << "futureObj.get() block main thread." << std::endl;

std::cout << futureObj.get() << std::endl;

th.join();

std::cout << "after join" << std::endl;

}

/*

std::bind

bind预先绑定的参数需要传具体的变量或值进去,对于预先绑定的参数,是pass-by-value的;[使用std::ref()可以pass by reference]

对于不事先绑定的参数,需要传std::placeholders进去,从_1开始,依次递增。placeholder是pass-by-reference的;

*/

int main(int argc, char* argv[])

{

//test_thread();

//test_thread_pool();

test_future();

return 0;

}

Reference

History

20180523: created.

Copyright

Post author: kezunlin

Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值