Thread: Thread Pool libraries or examples

  1. #1
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195

    Thread Pool libraries or examples

    I'm trying to utilize multi core technology as much as possible by throwing small, repetitive tasks onto a thread. When I read about this way of doing things, the thing that people referenced was a thread pool. Does anyone know any open source libraries, examples, or tutorials on how to write your own thread pooling architecture. I'm assuming its very easy, all you need is a queue and a bunch of worker threads and some sort of generic interface to access and process data.


    EDIT: I am developing this on a win32 box, but generic examples are welcome (ie. one that uses boost or vanilla win32 thread calls).
    Last edited by Mastadex; 08-23-2008 at 07:47 AM.
    Founder and avid member of the Internationsl Typo Associateion

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Why on earth would you post this question in the Win32 forum?

    Do you want a C or a C++ library?

    There's also Win2k's Job Object API, which is basically a thread pool.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    If you can use Boost's threads library, this is an OSS implementation. There's also the built-in threadpool in Win 2K+ which is, I think, to what CornedBee is referring.

  4. #4
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    Quote Originally Posted by CornedBee View Post
    Why on earth would you post this question in the Win32 forum?
    My engine is Windows only and I don't plan to move it to another platform anytime soon. At least, not until it's finished.

    Do you want a C or a C++ library?
    C++ Please.

    There's also Win2k's Job Object API, which is basically a thread pool.
    Good call. I'm looking at it now. How optimized is this library? Should I use it in a game engine which needs to be as close to real-time as possible?

    If you can use Boost's threads library, this is an OSS implementation. There's also the built-in threadpool in Win 2K+ which is, I think, to what CornedBee is referring.
    Thanks for the links!!
    Founder and avid member of the Internationsl Typo Associateion

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would go with the Job Object API and redesign if you find that it's lacking.

    Remember that Windows is not a Real-time OS, so real-time behaviour is a bit rough even at the best of times, and most attempts to "make it better" from an applications standpoint is going to be meaningless.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You may want to look at ACE. It is substantially more than a threading library (it is actually designed to support concurrent communications), but one of the many incidental capabilities it supports is multithreading and thread pools. Not exactly the easiest library to use, but the power is there.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Possibly use the thread pool API?

    EDIT: I'm not sure how you would use Job Objects for thread pooling. Per MSDN, they're used to manage a group of processes not threads.

    For example, the following code has a parent and child process. The parent being the process that started notepad.exe. The child is notepad.exe. If a job object were NOT used in the following sample, terminating the parent (the process that started notepad.exe) via the task manager would NOT terminate the child, notepad.exe. So, a job object is used to manage both the parent and child process. Using a job object now terminates notepad.exe when the parent process is terminated via the task manager.

    Code:
    #define _WIN32_WINNT 0x0500
    #include <windows.h>
    
    int main(void)
    {
        HANDLE                               hJob;
        JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 };
        PROCESS_INFORMATION                  pi   = { 0 };
        STARTUPINFO                          si   = { 0 };
        hJob = CreateJobObject(NULL, NULL);
        jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
        SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, &jeli, sizeof(jeli));
        si.cb = sizeof(si);
        CreateProcess( NULL, "notepad.exe",  NULL, NULL, FALSE, CREATE_SUSPENDED,NULL, NULL, &si, &pi );
        AssignProcessToJobObject(hJob, pi.hProcess);
        ResumeThread(pi.hThread);
        WaitForSingleObject( pi.hProcess, INFINITE );
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread );
        return 0;
    }
    Last edited by BobS0327; 08-24-2008 at 10:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 05:14 AM
  2. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  3. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 06:35 AM
  4. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 10:36 PM
  5. Creating a thread -- Passing it a function
    By cjschw in forum C++ Programming
    Replies: 0
    Last Post: 08-05-2003, 02:46 PM