条件变量

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

posted on 2017-04-25 17:24  Offen_Lou  阅读(110)  评论(0)    收藏  举报

导航