We help IT Professionals succeed at work.

translating some win32 thread stuff to pthreads

DJ_AM_Juicebox
on
199 Views
Last Modified: 2012-05-05
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
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
that is a shame!

Thanks
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.