0

使用 pthread 条件变量而不是 pthread_join 来模拟 Windows WaitForMultipleObject 有什么优势?

下面是使用 pthread_join 模拟 Windows WaitForMultipleObject 的 C++ 代码:

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

这是使用 pthread_cond_wait 模拟 Windows WaitForMultipleObject 的 C++ 代码:

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); 
}   

任何帮助是极大的赞赏。

4

0 回答 0