我具有同步的隊列,它提供一個條件變量。MonitorMixin條件變量 - >死鎖
當數據被添加到隊列即條件變量的信號。
我有5個主題:
Thread.new do
loop do
@queue.synchronize {
cond.wait_until { @queue.has_data? || @queue.finished? }
}
# some processing code that can also call @queue.enqueue
end
end
然後我做的:
@queue.enqueue some_data
@threads.each(&:join)
MyQueue#enqueue
看起來是這樣的:
def enqueue(data)
synchronize do
@pending << v unless queued?(data) || processed?(data) || processing?(data)
data_cond.signal
end
end
def finished?
@started && @processing.empty? && @pending.empty?
end
def has_data?
[email protected]?
end
而且我得到#join
deadlock detected
究竟這是如何導致死鎖,以及如何將一個解決?
什麼是你的代碼,增加了數據樣子的? –
嗨,弗雷德,我添加了#enqueue代碼 – glebm
has_data/finished方法是什麼樣的? –