2
votes

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?

1
How are you running work in the io service queue? Using run() or poll()? Do you use any io_service::work objects? - Sam Miller
I use work. I create few threads by thread_group and call run() inside every working thread. - fogbit

1 Answers

1
votes

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