Advantages of using pthread condition variables instead of pthread_join to emulate Windows WaitForMultipleObjects

334 views Asked by At

What are the advantages of using pthread condition variables instead of pthread_join to emulate Windows WaitForMultipleObject?

Here is the C++ code to emulate Windows WaitForMultipleObject using pthread_join:

int i;
for(i=0;i<threads;i++)
    pthread_join(threadids[i], NULL);

Here is the C++ code to emulate Windows WaitForMultipleObject using pthread_cond_wait:

mCritSect.Lock(); 
pthread_mutex_lock(&mMutex);
while (true) {
    // check if ThreadPool array has unused element
    for (unsigned ix(0); ix < NumberThreads; ix++) {
        if (ThreadPool[ix] == 0) {

            pthread_create(pThread, 
                (const pthread_attr_t*)0, 
                (void(*)(void*))ThreadFunction                   
                ChildList);

            pthread_mutex_unlock(&mMutex);
            mCritSect.UnLock();
            return ThreadPool[ix];
        }
    }
    // wait on mConditionVariable for ThreadPool array element to become available
    pthread_cond_wait(&mConditionVariable, &mMutex); 
}   

Any help is greatly appreciated.

0

There are 0 answers