2012-11-11 42 views
0
#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無鎖mutex.Is是正確的?呼叫調用pthread_cond_wait沒有鎖定互斥

回答

1

如果調用pthread_cond_wait()而調用線程已經鎖定了互斥鎖,則該程序具有未定義的行爲。任何事情都可能發生,包括崩潰。