为boost线程创建线程池
似乎有两种方法可以为 boost 线程创建线程池,因为 boost 线程不直接提供线程池。
第一个是
asio::io_service io_service;
asio::io_service::work work(io_service);
boost::thread_group threads;
for (std::size_t i = 0; i < my_thread_count; ++i)
threads.create_thread(boost::bind(&asio::io_service::run, &io_service));
io_service.post(boost::bind(an_expensive_calculation, 42));
io_service.post(boost::bind(a_long_running_task, 123));
io_service.stop();
threads.join_all();
第二个是使用这里的非官方线程池 http://threadpool.sourceforge.net/
void taskfunc();
// later on...
pool my_pool;
my_pool.schedule(&taskfunc);
我的问题是这两种方法中哪一种更好?两者之间有什么具体的优势吗?谢谢..
There seems to be two approaches to create thread pool for boost threads as boost thread does not provide thread pool directly.
The first one is
asio::io_service io_service;
asio::io_service::work work(io_service);
boost::thread_group threads;
for (std::size_t i = 0; i < my_thread_count; ++i)
threads.create_thread(boost::bind(&asio::io_service::run, &io_service));
io_service.post(boost::bind(an_expensive_calculation, 42));
io_service.post(boost::bind(a_long_running_task, 123));
io_service.stop();
threads.join_all();
The second one is using unofficial thread pool from here http://threadpool.sourceforge.net/
void taskfunc();
// later on...
pool my_pool;
my_pool.schedule(&taskfunc);
My question is which is a better approach among these two here? Are there any specific advantages of either over each other? Thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论