Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

ASKER

that is a shame!

Thanks