Lab 4.1: Condvar
1. 实验介绍
2. 条件变量
// 相关头文件请自行补全
int done = 0;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t c = PTHREAD_COND_INITIAILIZER;
void thread_exit() {
pthread_mutex_lock(&m); // 2.2
done = 1; // 2.1
pthread_cond_signal(&c); // 2.2
pthread_mutex_unlock(&m); // 2.2
}
void *child(void *arg) {
printf("child\n");
thread_exit();
return NULL;
}
void thread_join() {
pthread_mutex_lock(&m); // 2.2
while (done == 0) // 2.1 & 2.3
pthread_cond_wait(&c, &m); // 2.2
pthread_mutex_unlock(&m);// 2.2
}
int main(int argc, char *argv[]) {
printf("parent: begin\n");
pthread_t p;
pthread_create(&p, NULL, child, NULL);
thread_join();
printf("parent: end\n");
return 0;
}2.1 条件
2.2 锁
2.3 循环
3. 任务
4. 提交
Last updated