#include<pthread.h>
#include<stdio.h>
#include <errno.h>
pthread_cond_t done;
pthread_mutex_t mutex;
void*cond_wait(void*p){
while(1){
printf("%dwait\n",(int)p);
pthread_cond_wait(&done,&mutex);
printf("%dwakeup\n",(int)p);
}
}
int main(){
int status;
int i=1;
pthread_t p;
status=pthread_mutex_init(&mutex,NULL);
pthread_mutex_lock(&mutex);
pthread_cond_init(&done,NULL);
pthread_create(&p,NULL,cond_wait,(void*)1);
while(1){
sleep(1);
pthread_cond_signal(&done);
}
}
线程刚刚醒来,并没有被互斥锁阻塞,因为它不调用调用 pthread_cond_wait 而不锁定互斥锁。对吗?