C++11 Dynamic Threadpool C++11 Dynamic Threadpool multithreading multithreading

C++11 Dynamic Threadpool


  1. Start with maximum number of threads a system can support:

    int Num_Threads =  thread::hardware_concurrency();
  2. For an efficient threadpool implementation, once threads are created according to Num_Threads, it's better not to create new ones, or destroy old ones (by joining). There will be performance penalty, might even make your application goes slower than the serial version.

    Each C++11 thread should be running in their function with an infinite loop, constantly waiting for new tasks to grab and run.

    Here is how to attach such function to the thread pool:

    int Num_Threads = thread::hardware_concurrency();vector<thread> Pool;for(int ii = 0; ii < Num_Threads; ii++){  Pool.push_back(thread(Infinite_loop_function));}
  3. The Infinite_loop_function

    This is a "while(true)" loop waiting for the task queue

    void The_Pool:: Infinite_loop_function(){    while(true)    {        {            unique_lock<mutex> lock(Queue_Mutex);            condition.wait(lock, []{return !Queue.empty()});            Job = Queue.front();            Queue.pop();        }        Job(); // function<void()> type    }};
  4. Make a function to add job to your Queue

    void The_Pool:: Add_Job(function<void()> New_Job){    {        unique_lock<mutex> lock(Queue_Mutex);        Queue.push(New_Job);    }    condition.notify_one();}
  5. Bind an arbitrary function to your Queue

    Pool_Obj.Add_Job(std::bind(&Some_Class::Some_Method, &Some_object));

Once you integrate these ingredients, you have your own dynamic threading pool. These threads always run, waiting for jobs to do.


This should be simple to use: https://pocoproject.org/docs/Poco.ThreadPool.html


A thread pool always keeps a number of threads running, ready to accept work. Creating and starting a threads can impose a significant runtime overhead to an application. A thread pool helps to improve the performance of an application by reducing the number of threads that have to be created (and destroyed again). Threads in a thread pool are re-used once they become available again. The thread pool always keeps a minimum number of threads running. If the demans for threads increases, additional threads are created. Once the demand for threads sinks again, no-longer used threads are stopped and removed from the pool.

ThreadPool(    int minCapacity = 2,    int maxCapacity = 16,    int idleTime = 60,    int stackSize = 0);

This is very nice library and easy to use not like Boost :(

https://github.com/pocoproject/poco