CodeFree/6_solution/condition_variable/Readme.md

14 lines
523 B
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 1_CV_demo.cc
1、为什么线程A发通知前需要完成手动解锁
```c++
// 通知前完成手动解锁,以避免等待线程刚被唤醒就阻塞
ulk.unlock();
cv.notify_one();
```
原因:条件变量对象 cv 在执行 wait 函数时必须要获得锁。线程A与B共享锁且互相通知。如果A线程先发出通知而没有解锁那B线程的 wait 函数收到通知后,往下执行却没有获得锁,将继续陷入阻塞。
# 2_CV_demo.cc
介绍: 条件变量的 notify_all() 函数用法