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.