In my C ++ program, I have a CEvent class with trigger and wait functions based on pthreads (works on Linux). The implementation is pretty obvious (i.e. many examples on the Internet) if there is one waiting process. However, now I need to fulfill the requirement that several threads are expected for the event, and ALL SHOULD wake up with reliability when calling trigger (). As a second condition, only those threads should appear that were waiting when trigger () was called.
My current code is:
void CEvent::trigger() {
pthread_mutex_lock(&mutex);
wakeUp = true;
pthread_cond_broadcast(&condition)
pthread_mutex_unlock(&mutex);
wakeUp = false;
}
void CEvent::wait() {
pthread_mutex_lock(&mutex);
while (!wakeUp)
pthread_cond_wait(&condition, &mutex)
pthread_mutex_unlock(&mutex);
}
, , , wakeUp false. reset wakeUp ( ) , wait(), , . wakeUp = false mutext .
: * pthread_cond_broadcast? , , ? * ?
Steve