#include #include pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; void *thread1(void *); void *thread2(void *); int i=1; main(void) { pthread_t t_a; pthread_t t_b; pthread_create(&t_a,NULL,thread1,(void *)NULL); pthread_create(&t_b,NULL,thread2),(void *)NULL); pthread_join(t_b, NULL); pthread_mutex_destroy(&mutex); pthread_cond_destroy(&cond); exit(0); } void *thread1(void *junk) { for(i=1;i<=9;i++) { pthread_mutex_lock(&mutex); if(i%3==0) pthread_cond_signal(&cond); else printf("thead1:%d/n",i); pthread_mutex_unlock(&mutex); printf("Up Unlock Mutex/n"); usleep(50); } } void *thread2(void *junk) { while(i<9) { pthread_mutex_lock(&mutex); if(i%3!=0) pthread_cond_wait(&cond,&mutex); printf("thread2:%d/n",i); pthread_mutex_unlock(&mutex); printf("Down Ulock Mutex/n"); usleep(50); }
版权申明:
转载文章请注明原文出处http://blog.csdn.net/feiyinzilgd/archive/2010/12/30/6108417.aspx
并请联系谭海燕本人或者前往谭海燕个人主页留言