8 – Download Source And Examples

Download Source Code

KMThreadPool Library Source Code (September 29th, 2010)

  • Compile the thread pool into a library file to include in all your projects! Includes source code, so even if you don’t want it as a lib file, you can just simply add the source code. Make sure you read the README!

KMThreadPool Source Code (Old – July 5th, 2010)

  • Contains a few bugs, but still functions. Really up here for legacy.

>> Next, Uhh… Nothing! Run along, you little scamp! >>

17 thoughts on “8 – Download Source And Examples”

  1. This is well written and focused material. The only suggestion for change would be to have the task threads deschedule themselves if there is nothing in the task queue. This would also require the code to put something in the task queue to wake up the descheduled worker threads. This would allow the load the CPU is under to vary based on the “tasks” that needed to be done and not artificially do the “Sleep(1)” which could have you sleeping when you need to work (a true programmers nightmare 🙂 )

    1. So what would you recommend? An idea was to have a sort of signal handle inside the threads that gets modified by the thread pool. If there are tasks in the thread pool, then the thread pool signals all the threads into a “work mode”.

      If the handle is in work mode, the main thread loop would process as it does now, but in non-work mode, the thread would sit in a spin-lock or some similar design.

      Do I have the basic idea down?

  2. Amazing tutorial, I loved it! Thanks man truly appreciated 🙂

    Will go on and make an implementation in Python first and then one in C++ aswell for myself =}

    Thansk again!

    PS: I bit more explanation would be nice about hardware threads / virtual threads instead of a link to wiki, since the rest of the tutorial was so lovely, this part felt abit lacking :(, perhaps try to approach the problem from a game engine perspective and explain it from there (render thread, update thread, x num task threads for animation, particles etc)

    1. Thanks for the praise! I’m glad you enjoyed it.

      As for the hardware/virtual threads part, I figured that this information would have been known prior to making the threadpool, but thinking back, I could have given at least a brief comedic summary. 😛

      And I wanted to keep the threadpool as modular as possible, and not confuse anyone by saying, “This is how it would work in a physics engine…” or “Rendering can be done by…” or anything like that. I wanted to lay down the bare bones of a simple, generic threadpool.

      I’m waiting,now, for C++11 to go mainstream so I can use the built-in threads and make a Threadpool 2.0, which I’m going to make safer and more robust.

  3. Hey!
    Thanks for your code. I thing it is a fair implementation of one Threadpool.:)
    A really thread pool can see when threads from the pool are being blocked and can create and dispatch more work items. In the future, it can see if are more threads than available processors and can destroy some overhead threads.

    If was i, i used a Semaphore with 0 units, that, when i don’t have any work item, the threads blocks on this Kernel Object. When you put one work item, you signal the semaphore releasing one thread at time.

    By doing in this way, you are not removing and adding this thread to the ready threads queue on the scheduler and so, windows don’t schedule that thread until not recieves any signal.

    No CPU time is wasting.

    1. That’s a very interesting idea, and I wanted to do something similar, however I could not find a suitable way to get the number of threads available on any one processor. Any suggestions would be greatly appreciated!

      1. You can do in this way:

        SYSTEM_INFO sysinfo;
        GetSystemInfo( &sysinfo );

        numCPU = sysinfo.dwNumberOfProcessors;

        You control more efficiently the overhead associated with your pool and avoid context switching all the time (Where optimal performance is having threads as the number of processors, maybe more when some of the workers gets blocked)

      2. “….however I could not find a suitable way to get the number of threads available on any one processor. Any suggestions would be greatly appreciated!”

        Thanks a lot for an excellent work shared with the public.

        The limit in reality is the total number of cores in the machine. If you have a 4 core machine and massive work in each worker thread all time then the best number of threads to you use is: 3. One left for all the rest. Sounds strange, but when I created multithreaded realtime applications for investment banking (yeah I know.. but I don’t work for financial industry any more) this was the result of all our experiments.
        On the other hand you can threads until the OS can’t create any more of them. But remember that they all share the available hardware cores. The machine is dead when the thread scheduler of the OS can’t switch any more between all the thousands of CPU threads.

        Additionally you can move work to a GPU cluster. In in any case you have to return to the memory controlled by the CPU. So if more power is required a machine with more CPU cores is needed.

      3. Thanks for developing and sharing this tutorial. I integrated this into my project. I initialize the thread-pool with two threads. Assign task and data to a thread in the thread-pool. I am stuck in the following scenario:

        My task starts two more processing threads from the pool-thread. I need to change the status of the pool-thread after all my processing-threads in the pool-thread are completed.

        How can I do that? What are the changes needed in thread-pool library?

  4. Hey, I’ve found a bug in your code. Let’s have a look at the following routine in KMThread_Win32.cpp:

    DWORD KMThread::ThreadProc()
    {
    //…

    // The main thread-loop. As long as this loop
    // is running, the thread stays alive.
    while(m_brunning)
    {
    // The thread pauses when it finishes a task.
    // Adding a task resumes it.
    if(!m_bpaused)
    {
    m_lock.Lock();
    {
    KMTaskFunc task = m_ptask->GetTask();
    IKMTaskData* data = m_ptask->GetData();
    // Run the actual task
    task(data);
    // Task is complete.
    m_bpaused = true;
    delete m_ptask;
    m_ptask = NULL;
    }
    m_lock.Unlock();
    }
    }

    //…
    }

    In fact, when no task is assigned to a serving thread, the above code consumes a lot of CPU resource. I think we better off employing an additional synchronization mechanism. 🙂

  5. hello Gentleman
    i must say thank you for every thing in this wonderfull tutorial
    im begginer in multi threading and its perfect for me to start
    good lock man and be happy
    thank you

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