使用pthread条件变量而不是pthread_join来模拟Windows WaitForMultipleObject有什么好处?
这是使用模拟Windows WaitForMultipleObject的C ++代码 在pthread_join:
int i;
for(i=0;i<threads;i++)
pthread_join(threadids[i], NULL);
这是使用模拟Windows WaitForMultipleObject的C ++代码 调用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);
}
非常感谢任何帮助。