Re: [问题] thread-safe queue

楼主: suhang (suhang)   2019-04-26 12:40:06
※ 引述《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
作者: stucode   2019-04-26 15:33:00
这样的话不会 overflow 可是会卡死,因为 Ta 的 notify已经被 Tb 吃掉了,但 Tb 什么都没做又继续回去等待,且T1 也还在等 notify 来唤醒他。造成伫列中还有东西,可是所有 threads 都在睡的局面。

Links booklink

Contact Us: admin [ a t ] ucptt.com