#include <stdio.h> #include <pthread.h> #include <signal.h> pthread_mutex_t m1, m2; pthread_cond_t c1,c2; pthread_t t1, t2, t3; void* r1(void *arg) { sleep(10); //睡眠10秒 pthread_cond_signal(&c1); printf("t1 finish\n"); while(1); } void* r2(void *arg) { sleep(15);//睡眠15秒 pthread_cond_signal(&c2); printf("t2 finish\n"); while(1); } void* r3(void *arg) { pthread_cond_wait(&c1, &m1); pthread_cond_wait(&c2, &m2); printf("finish\n");//15秒后线程打印 } main() { pthread_mutex_init(&m1, 0); pthread_mutex_init(&m2, 0); pthread_cond_init(&c1, 0); pthread_cond_init(&c2, 0); pthread_create(&t1, 0, r1, 0); pthread_create(&t2, 0, r2, 0); pthread_create(&t3, 0, r3, 0); while(1); } |