}
至关重要的绚烂的结果分割线
thead1:1
Up Unlock Mutex
thead1:2
Up Unlock Mutex
Up Unlock Mutex
thread2:3
Down Ulock Mutex
thead1:4
Up Unlock Mutex
thead1:5
Up Unlock Mutex
Up Unlock Mutex
thread2:6
Down Ulock Mutex
thead1:7
Up Unlock Mutex
thead1:8
Up Unlock Mutex
Up Unlock Mutex
thread2:9
Down Ulock Mutex
HOW IT WORKS
i不是三的倍数的时候.
thread2条件变量阻塞,释放Mutex
thread1加锁,打印thread1:i,释放锁,打印"Up Unlock Mutex"
i为3的倍数的时候,
thread1,加锁,条件变量通知,唤醒条件变量阻塞线程,打印"Up Unlock Mutex"
thread2,被唤醒,加锁,打印"thread2:i",释放锁,打印"Down Ulock Mutex"
所以说函数pthread_cond_wait()调用时,不仅对条件变量起作用,还对互斥锁有作用!
唯美的好书推荐线
"Beiginning Linux Programming" by Neil Matthrew & Richard Stones
You can choose the English version or Chinese version, but I recommend the English one. Maybe it' easier to understand what the authers say than the Chinese one , even if you're Chinese.
来自:chinaunix技术社区,
如需转载请注明出处。