如何在C++中使用boost来创建线程池,以及如何将任务分配给线程池?如何在C++中使用boost来创建线程池?
回答
该过程非常简单。首先创建一个asio :: io_service和一个thread_group。使用链接到io_service的线程填充thread_group。使用boost::bind函数将任务分配给线程。
要停止线程(通常在退出程序时),停止io_service并加入所有线程。
你应该只需要这些标题:
#include <boost/asio/io_service.hpp>
#include <boost/bind.hpp>
#include <boost/thread/thread.hpp>
这里有一个例子:
/*
* Create an asio::io_service and a thread_group (through pool in essence)
*/
boost::asio::io_service ioService;
boost::thread_group threadpool;
/*
* This will start the ioService processing loop. All tasks
* assigned with ioService.post() will start executing.
*/
boost::asio::io_service::work work(ioService);
/*
* This will add 2 threads to the thread pool. (You could just put it in a for loop)
*/
threadpool.create_thread(
boost::bind(&boost::asio::io_service::run, &ioService)
);
threadpool.create_thread(
boost::bind(&boost::asio::io_service::run, &ioService)
);
/*
* This will assign tasks to the thread pool.
* More about boost::bind: "http://www.boost.org/doc/libs/1_54_0/libs/bind/bind.html#with_functions"
*/
ioService.post(boost::bind(myTask, "Hello World!"));
ioService.post(boost::bind(clearCache, "./cache"));
ioService.post(boost::bind(getSocialUpdates, "twitter,gmail,facebook,tumblr,reddit"));
/*
* This will stop the ioService processing loop. Any tasks
* you add behind this point will not execute.
*/
ioService.stop();
/*
* Will wait till all the threads in the thread pool are finished with
* their assigned tasks and 'join' them. Just assume the threads inside
* the threadpool will be destroyed by this method.
*/
threadpool.join_all();
['boost :: asio :: io_service :: work'](http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/io_service__work.html)object is a critical piece使其正常运作。另外['io_service :: stop()'](http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/io_service/stop.html)将阻止执行任何其他任务,而不管任务何时发布到'io_service'中。例如,'getSocialUpdates()'在'stop()'之前被添加到'io_service'队列中,如果调用'stop()'时不是中等执行,那么它将保持排队。 –
您需要在创建工作线程之前创建'work'对象*,否则它们可能会立即终止而无需执行任何操作。 – Miral
@TannerSansbury其实这个配方让我非常困惑,因为在io_service.stop()之后,所有未完成的工作都会被杀死。正确的方法应该是除去ioservice.stop(),但是破坏工作对象,然后调用threadpool.join_all()让所有作业完成。 – CyberSnoopy
我知道你喜欢的代码。
我的版本
namespace bamthread
{
typedef std::unique_ptr<boost::asio::io_service::work> asio_worker;
struct ThreadPool {
ThreadPool(size_t threads) :service(), working(new asio_worker::element_type(service)) {
while(threads--)
{
auto worker = boost::bind(&boost::asio::io_service::run, &(this->service));
g.add_thread(new boost::thread(worker));
}
}
template<class F>
void enqueue(F f){
service.post(f);
}
~ThreadPool() {
working.reset(); //allow run() to exit
g.join_all();
service.stop();
}
private:
boost::asio::io_service service; //< the io_service we are wrapping
asio_worker working;
boost::thread_group g; //< need to keep track of threads so we can join them
};
}
代码至少要使用它:
{
bamthread::ThreadPool tp(n_threads);
BOOST_FOREACH(int y, boost::irange(starty, endy, step)){
int im_x = 0;
BOOST_FOREACH(int x, boost::irange(startx, endx, step)){
tp.enqueue (boost::bind(&camera_view_depth::threaded_intersection, this,
intersections,
intersected,
im_x,
im_y,
_faces, x, y));
++im_x;
}
++im_y;
}
}
- 1. 使用Boost线程和io_service创建线程池
- 2. 如何使用Boost创建线程
- 3. 在c中创建线程池#
- 4. 如何创建线程池?
- 5. 如何在nodejs中创建线程池?
- 6. boost线程池
- 7. .NET中创建线程池(C#)
- 8. 在C++中使用Boost的线程池无法正常工作
- 9. 如何在C++中使用boost来创建正则表达式?
- 10. C#创建动态类并在线程池中使用它
- 11. 如何在Java中使用LinkedBlockingQueue创建线程池?
- 12. 如何在boost :: asio线程池中创建任务可取消/可中断?
- 13. 何时创建线程池?
- 14. 使用System.Threading.Tasks.Parallel在线程池中创建新线程?
- 15. 你可以选择线程池中的线程来执行(boost)
- 16. C++ Boost :: ASIO线程池问题
- 17. 如何创建和线程池
- 18. hystrix如何创建其线程池
- 19. 使用boost库在vc6中创建多线程应用程序?
- 20. 如何创建迭代boost线程?
- 21. 使用在线程池线程上创建的BitmapImage
- 22. 在boost线程池库中的问题
- 23. 在linux下使用boost :: thread创建boost :: asio工作线程
- 24. boost :: asio,线程池和线程监视
- 25. C++ - 如何使用boost来恢复线程?
- 26. 的boost ::线程 - 线程创建问题
- 27. 使用线程池进行仿真:boost-thread和boost-asio
- 28. 线程池在C#
- 29. 如何在Play 2.5.x中创建线程池?
- 30. 在C#中创建线程
[创建使用boost线程池(可能重复http://stackoverflow.com/问题/ 4084777 /创建线程池使用提升) –
唯一的事情是它不允许我回答其他问题,并允许和鼓励自我回答。 –
你应该能够发布[其他问题]的答案(http://stackoverflow.com/questions/4084777/creating-a-thread-pool-using-boost),它不是关闭或[保护] (http://meta.stackexchange.com/questions/52764/what-is-a-protected-question)。 –