【发布时间】:2016-03-22 03:11:06
【问题描述】:
看看answers like this one,我们可以做这样的事情:
boost::asio::io_service ioService;
boost::thread_group threadpool;
{
boost::asio::io_service::work work(ioService);
threadpool.create_thread(boost::bind(&boost::asio::io_service::run, ioService));
threadpool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService));
ioService.post(boost::bind(...));
ioService.post(boost::bind(...));
ioService.post(boost::bind(...));
}
threadpool.join_all();
但是,就我而言,我想做类似的事情:
while (condition)
{
ioService.post(boost::bind(...));
ioService.post(boost::bind(...));
ioService.post(boost::bind(...));
threadpool.join_all();
// DO SOMETHING WITH RESULTS
}
但是,boost::asio::io_service::work work(ioService) 行不合适,据我所知,如果不重新创建池中的每个线程,我无法重新创建它。
在我的代码中,线程创建开销似乎可以忽略不计(实际上比以前基于互斥锁的代码性能更好),但有没有更简洁的方法来做到这一点?
【问题讨论】:
标签: c++ boost-asio boost-thread