phtread条件变量pthread_cond_t初始化方式

1.静态方式
初始化方法:
int x,y;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;//在栈上初始化
Waiting until x is greater than y is performed as follows:
pthread_mutex_lock(&mut);
while (x <= y) {
        pthread_cond_wait(&cond, &mut);
}
/* operate on x and y */
pthread_mutex_unlock(&mut);

2.动态方式
pthread_cond_t *cond = NULL;
cond = malloc(sizeof(pthread_cond_t));//在堆上初始化

phtread_cond_destroy(cond);//释放条件变量

 

你可能感兴趣的:(linux,基础知识)