简体   繁体   中英

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();
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM