zl程序教程

您现在的位置是:首页 >  后端

当前栏目

phtread条件变量pthread_cond_t初始化方式

变量 方式 条件 初始化 pthread
2023-09-14 09:09:56 时间
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);//释放条件变量