I have a \"main\" function that performs many small, independent tasks each once per time step. However, after each time step, I must wait for all of the tasks to complete befor
You may use futures for data processing and synchronize with them using boost::wait_for_all(). This will allow you to operate in terms of parts of work done, not threads.
int process_data() {...}
// Pending futures
std::vector> pending_data;
for(int i = 0; i < numSmallTasks; ++i)
{
// Create task and corresponding future
// Using shared ptr and binding operator() trick because
// packaged_task is non-copyable, but asio::io_service::post requires argument to be copyable
// Boost 1.51 syntax
// For Boost 1.53+ or C++11 std::packaged_task shall be boost::packaged_task
typedef boost::packaged_task task_t;
boost::shared_ptr task = boost::make_shared(
boost::bind(&process_data, i, theTime));
boost::unique_future fut = task->get_future();
pending_data.push_back(std::move(fut));
io_service.post(boost::bind(&task_t::operator(), task));
}
// After loop - wait until all futures are evaluated
boost::wait_for_all(pending_data.begin(), pending_data.end());