作者suhang (suhang)
看板Python
标题Re: [问题] thread-safe queue
时间Fri Apr 26 12:40:06 2019
※ 引述《Yshuan (倚弦)》之铭言:
: que size已满. 三个Thread.
: producer: Ta, Tb.
: consumer: T1.
: 0. producer都先启动. 并且停在wait()
: 1. T1执行, 执行完後notify Ta.
: 2. Ta结束wait, 执行完後notify Tb
: 因为你的condition只有一个, 因此造成producer notify producer.
: 3. Tb结束wait. 执行并que overflow.
Tb结束wait之後,应该是执行 第二行的while loop检查
检查发现que full, 就接着执行第三行的wait(),继续回去等待
你说的情况应该是第二行写成if的情况才会发生吧?
1. with self.condition:
2. while len(self.que) == self.cap:
3. self.condition.wait()
thanks for helping me
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 76.169.162.97
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1556253608.A.932.html
※ 编辑: suhang (76.169.162.97), 04/26/2019 12:41:14
1F:→ stucode: 这样的话不会 overflow 可是会卡死,因为 Ta 的 notify 04/26 15:33
2F:→ stucode: 已经被 Tb 吃掉了,但 Tb 什麽都没做又继续回去等待,且 04/26 15:33
3F:→ stucode: T1 也还在等 notify 来唤醒他。造成伫列中还有东西,可是 04/26 15:34
4F:→ stucode: 所有 threads 都在睡的局面。 04/26 15:34