translating some win32 thread stuff to pthreads

Posted on 2007-10-12
Last Modified: 2012-05-05

I have this bit of code that I've been using for awhile which creates a thread pool of 100 threads. When requests come in to create a new thread, the new thread is created only if there is an open slot in the thread pool. Otherwise the function waits for a slot to become available and will create the thread in that slot:

    m_nOpenSlot = 0;
    m_nOpenThreads = 0;
    m_vThreads.resize(100, 0); // vector<HANDLE>, the thread pool.

int MyClass::GetMaxNumberOfAllowedThreads()
    return 100;

void MyClass::CreateNewThread()
    // If there are already 100 threads open,  wait until one finishes then use that opened slot.
    if (m_nOpenThreads >= GetMaxNumberOfAllowedThreads()) {
        DWORD dwRes = WaitForMultipleObjects(GetMaxNumberOfAllowedThreads(), &m_vThreads[0], FALSE, INFINITE);
        m_nOpenSlot = dwRes - WAIT_OBJECT_0;

    // Let derived classes create their own specialized thread data to handle the association.
    // It is treated as a base class type.
    SomeData* p = new SomeData();

    m_vThreads[m_nOpenSlot] = (HANDLE)_beginthreadex( (void *) 0,
                                                    (unsigned) 0,
                                              (PTHREAD_START) &ThreadFunction,
                                                      (void *) p,
                                                    (unsigned) 0,


I don't know what the equivalents is in pthreads for:

    DWORD dwRes = WaitForMultipleObjects(GetMaxNumberOfAllowedThreads(), &m_vThreads[0], FALSE, INFINITE);
    m_nOpenSlot = dwRes - WAIT_OBJECT_0;

is there some one to one mapping for it or a completely new methodology?

Question by:DJ_AM_Juicebox
    LVL 86

    Accepted Solution

    Unfortunately, there is no such equivalent for pthreads (see e.g. the discussion at As an alternative, take a look at the article ("A Thread Pool compatible with Win32 and pthreads API")

    Author Comment

    that is a shame!


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now