How to make join on boost::asio::io

Here is the problem:

In the main thread (io - boost::asio::io_service):

io.post(functor1, callback1)
....
io.post(functorN, callbackN)

io.join() <--- waiting while all the task to be processed and continue to execute the program

The code is executed in a loop. boost::thread_group would perfectly match, but it creates new threads all the times, while I want to create working threads only once and dispatch tasks to them, exactly as asio does. All pool threads I've seen are just wrapers around vector of threads + io_service, it can't be used in the way I've shown above.

So, how can I make "join" for boost::asio?


So,

work_.reset();
thread_group_.join_all();

should be enough. The usual approach is to have

boost::unique_ptr<io_service::work> work_; // non-copyable

or

boost::optional<io_service::work> work_; // copyable
链接地址: http://www.djcxy.com/p/62682.html

上一篇: 服务与未完成的完成处理程序

下一篇: 如何在boost :: asio :: io上进行连接