C++ Win32 Thread Pool Manager

Here is the step-by-step overview of how to implement a thread pool in C++ on a Win32 platform!

This is a newly updated tutorial with many optimizations (Thanks to Gary at Incinerator Studios), and now I’ve made a better tutorial and a library file so you can include the thread pool in any project.

Just follow the step-by-step instructions and you’ll be on your way to being a fluent thread-master!

  1. Introduction
  2. Concepts and Facts
  3. Step 1 – Utilities, Locks and Algorithms
  4. Step 2 – Setting Up the Threads
  5. Step 3 – Enter Thread Pool
  6. Step 4 – How To Use
  7. Conclusion
  8. Download Source and Examples

>> Next, Introduction >>

8 thoughts on “C++ Win32 Thread Pool Manager

  1. plutus2sutulp April 9, 2013 / 11:46 AM

    Dear MR Maggio

    I notified the main.cpp in the KMThreadPool,like this:

    /*
    // Add tasks to our threadpool.
    for(int i = 0; i AddTask(Sample1, data);
    else
    pool->AddTask(Sample2, data);
    }
    */
    // See how long it takes to execute
    Timer elapsed;
    pool->BeginProcessing();
    elapsed.Reset();
    while(true)
    {
    // Check to see if all tasks are completed. This is only for this sample program.
    // In others, the pool will be shut down once the program exits.
    //if( pool->getActiveThreads()==pool->getNumDormantThreads() && !pool->hasWaitingTasks()) {
    // break;
    //}
    i = i % 1000;
    SampleTask* data = new SampleTask(i);
    if(i%2==0)
    pool->AddTask(Sample1, data);
    else
    pool->AddTask(Sample2, data);
    i ++;

    // Cut-off in the middle of execution
    if(GetAsyncKeyState(VK_ESCAPE)) {
    break;
    }
    }

    but a exception happened in dbgheap.c at line 239: pvBlk = _heap_alloc_dbg_impl(nSize, nBlockUse, szFileName, nLine, errno_tmp);

    the exception said: unprocess exception 0xC0000005: a access collision happened when read 0x00000000,

    if I know your email, so I send the the main.cpp I notified and the exceptional picture I cut to you.

    my email: liqiang.deng@gmail.com

    I hope can get your help to fix that.

    Thank you very much.

    • KeithM April 9, 2013 / 4:31 PM

      I’ve sent an email. Once we get this resolved, I’ll post it here.

      • Alon June 1, 2015 / 7:19 AM

        Hi,Have you solved the Access-Violation exception ? I have the same problem …

  2. owallentin October 10, 2013 / 3:38 PM

    Hello! Just wanted to pop by and say thank you for this great article on Thread Pool Management! I am working with multi-threading in a course at University and your explanation really helped me understand how to manage threads in a better way 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s