translating some win32 thread stuff to pthreads

Hi,

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:


MyClass::MyClass()
{
    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;
        m_nOpenThreads--;
        CloseHandle(m_vThreads[m_nOpenSlot]);
    }

    // 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,
                                                               0);

    m_nOpenSlot++;
    m_nOpenThreads++;
}


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?

Thanks
DJ_AM_JuiceboxAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
Unfortunately, there is no such equivalent for pthreads (see e.g. the discussion at http://developers.slashdot.org/article.pl?sid=07/02/26/1211220). As an alternative, take a look at the article http://www.codeproject.com/threads/threads.asp ("A Thread Pool compatible with Win32 and pthreads API")
0
 
DJ_AM_JuiceboxAuthor Commented:
that is a shame!

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.