条件变量
// def
pthread_mutex_t mutex;
pthread_cond_t cond;
bool bCondition;
// init
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
bCondition = false
// thread 1
pthread_mutex_lock(&mutex);
while (!bCondition)
{
pthread_cond_wait(&cond, &mutex);
}
// do something
pthread_mutex_unlock(&mutex);
// thread 1-end
// thread 2
pthread_mutex_lock(&mutex);
bCondition = true;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
// thread 2-end
// destory
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
浙公网安备 33010602011771号