Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::asio io_service thread pool

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?

like image 282
David Avatar asked Oct 31 '11 17:10

David


People also ask

Does boost asio use threads?

If threads are used, several functions can be executed concurrently on available CPU cores. Boost. Asio with threads improves the scalability because your program can take advantage of internal and external devices that can execute operations independently or in cooperation with each other.

What is Boost asio?

Boost. Asio is a cross-platform C++ library for network and low-level I/O programming that provides developers with a consistent asynchronous model using a modern C++ approach. Overview. An overview of the features included in Boost. Asio, plus rationale and design information.

What is Boost :: asio :: Post?

boost::asio::post takes any callable object. Requirements for such object you can find here. There are many ways to achive what you want: [1] lambda expressions boost::asio::post(tp, [i]{ printProduct(i); });

Who is using Boost asio?

3 Answers. Show activity on this post. The systems software for managing an IBM Blue Gene/Q supercomputer uses Boost. Asio extensively.


Video Answer


1 Answers

run() is a blocking call, and will execute all events that it can before returning. It will only return if there are no more events to handle. Once it returns, you must call reset() on the io_service before calling run() again.

You can have multiple threads calling run() - this is not a problem, and you don't need the infinite loop as long as the io_service has some work to do. The normal pattern for this is to create a work object on the io_service which will force run() to never return. This does mean that you explicitly have to call stop() on the io_service when you are done as it will never naturally exit.

If you setup a work on the io_service, it will never naturally exit and therefore you will never need to call reset().

work some_work(m_IoService); // this will keep the io_service live.

for( unsigned int i = 0; i < numThreads; ++i )
{
  m_Threads.create_thread(
    [&]()
    {
      m_IoService.run();
    });
}

Now all threads are dispatching events on the io_service

// Now post your jobs
m_IoService.post(boost::bind(...)); // this will be executed in one of the threads
m_IoService.post(boost::bind(...)); // this will be executed in one of the threads
m_IoService.post(boost::bind(...)); // this will be executed in one of the threads

m_IoService.stop(); // explicitly stop the io_service
// now join all the threads and wait for them to exit
like image 136
Nim Avatar answered Oct 19 '22 23:10

Nim