Boost logo

Boost Users :

Subject: [Boost-users] Is thread pool using asio and thread_group working as intended?
From: Jonas Nilsson (mjc.nilsson_at_[hidden])
Date: 2012-04-12 11:16:47


Hello,

I have found several sites illustrating the use of boost:asio::io_service together with boost::thread_group to create a simple thread pool system. I put together my own minimum example to test it:

asio::io_service io_service;
asio::io_service::work work(io_service);
boost::thread_group threads;
for (std::size_t i = 0; i < running_threads; ++i)
{
    threads.create_thread(boost::bind(&asio::io_service::run, &io_service));
}
for (int u = 0; u < work_units; u++)
{
    io_service.post(boost::bind(WorkFunc, u));
}
io_service.stop();
threads.join_all();

When running the code, I find that all worker threads are killed when the following line is executed: io_service.stop();.
Does the code work as intended? My impression from the comments to the examples I have seen is that it does not work as intended on my system.

If the code does work as intended, is there a simple non polling method to determine when all work units are completed?

/Jonas


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net