日期:2014-05-16  浏览次数:20635 次

linux中程序中的3个线程 怎么让线程1控制线程 2 线程3
线程1 2 3 都在同时运行,

线程1 突然来了个一个通知,需要让线程2 与 3 同时阻塞在那里,或者空转也行(这个可以加标记的可以实现)!
现在是想同时阻塞等待,所以请问大侠们怎么弄?

------解决方案--------------------
pthread_cond_t cond
pthread_mutex_t mutex
volatile int flag = 0
//init cond and mutex

thread1:
//receive  a message
flag = 1
...
...
//going to terminate
pthread_cond_broadcast(&cond)

thread2:
//begin
pthread_mutex_
if(flag) {
     pthread_mutex_lock(&mutex)
     pthread_cond_wait(&cond, &mutex)
     pthread_mutex_unlock(&mutex)
}

thread3:
//ditto