[问题] thread-safe queue

楼主: suhang (suhang)   2019-04-26 04:01:59
https://paste.ubuntu.com/p/cWsFNYcGpQ/
先写了MyQueue1 用一个condition
consumer thread透过condition判断que empty ,就wait, release lock
producer thread透过同一个condition 取得lock, 放东西到que, and notify consumer
但是看了python and java source code
都是用两个condition (not_empty / not_full)
为什么要这么做呢?我的作法应该也行得通,难道是效率问题?
看不透,请大家解疑,感谢
作者: benchen0812 (あBen)   2019-04-26 06:22:00
我不确定 但我觉得应该是你这样每次wake get() put()同时被wake一次? 然后如果 2 condition get()->阿说错 put()只需要notify get() 因为你在 len(que)>= cap: wait 的时候 只有当len(que)< cap 才会放出LOCK, 这时候你的len(que) = cap - 1. 然后你在que.append 这时len(que) = cap, 你只需要叫get(), 因为你叫put他只会继续入睡(len(put) = cap)如果有错请指正XD
作者: Yshuan (倚絃)   2019-04-26 08:30:00
size=1的时候, 2个thread去get, 一个卡wait, 一个过了condition相同, 过了的那个 nottify到 wait的那个, 然后?size=Full. Ta, Tb: put and wait. T1: get and sig(Ta).T2: put and acquire first then Ta, Tb, finally sig(Tb)Ta, Tb wait and put, size overflow.更正, 不需要T2. Ta wake and sig(Tb). 就炸了

Links booklink

Contact Us: admin [ a t ] ucptt.com