fogbit
fogbit

Reputation: 2063

How to make join on boost::asio::io_service?

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?

Upvotes: 2

Views: 1325

Answers (1)

sehe
sehe

Reputation: 392989

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

Upvotes: 1

Related Questions