Search code examples
c++boostthreadgroup

Boost thread_group thread limiter


Am trying to limit the number of threads at anytime to be equal to utmost the number of cores available. Is the following a reasonable method? Is there a better alternative? Thanks!

    boost::thread_group threads;
    iThreads = 0;

    for (int i = 0; i <  Utility::nIterations; i++)
    {

        threads.create_thread(
            boost::bind(&ScenarioInventory::BuildInventoryWorker, this,i));

        thread_limiter.lock();
        iThreads++;
        thread_limiter.unlock();

        while (iThreads > nCores)
            std::this_thread::sleep_for(std::chrono::milliseconds(1)); 

    }

threads.join_all();


void ScenarioInventory::BuildInventoryWorker(int i)
{
    //code code code....

    thread_limiter.lock();
    iThreads--;
    thread_limiter.unlock();
}

Solution

  • What you are likely looking for is thread_pool with a task queue.

    Have a fixed number of threads blocking an queue. Whenever a task is pushed onto the queue a worker thread gets signalled (condition variable) and processes the task.

    That way you

    • don't have the (inefficient) waiting lock
    • don't have any more threads than the "maximum"
    • don't have to block in the code that pushes a task
    • don't have redundant creation of threads each time around

    See this answer for two different demos of such a thread pool w/ task queue: Calculating the sum of a large vector in parallel