2

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?

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
fogbit
  • 1,961
  • 6
  • 27
  • 41
  • How are you running work in the io service queue? Using run() or poll()? Do you use any io_service::work objects? – Sam Miller Feb 20 '15 at 15:48
  • I use ``work``. I create few threads by ``thread_group`` and call ``run()`` inside every working thread. – fogbit Feb 20 '15 at 15:50

1 Answers1

1

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
sehe
  • 374,641
  • 47
  • 450
  • 633