What's the proper usage of settings up a thread pool for io_service? These 2 statements from the documentation are throwing me off:
io_service::run
A normal exit from the run() function implies that the io_service object is stopped (the stopped() function returns true). Subsequent calls to run(), run_one(), poll() or poll_one() will return immediately unless there is a prior call to reset().
io_service::reset
This function must be called prior to any second or later set of invocations of the run(), run_one(), poll() or poll_one() functions when a previous invocation of these functions returned due to the io_service being stopped or running out of work.
Here's what I'm currently doing:
boost::thread_group m_Threads;
boost::asio::io_service m_IoService;
boost::barrier m_Barrier(numThreads);
for( unsigned int i = 0; i < numThreads; ++i )
{
m_Threads.create_thread(
[&]()
{
for(;;)
{
m_IoService.run();
if( m_Barrier.wait() ) // will only return true for 1 thread
{
m_IoService.reset();
}
m_Barrier.wait();
}
});
}
m_IoService.stop();
m_Threads.interrupt_all();
m_Threads.join_all();
Everything seems to work fine if I just put m_IoService.run()
in an infinite loop (which the documentation seems to indicate should not be the case). What's the correct way?