An answer to this question by railscard and the doc for ConditionVariable
suggest code similar to the following:
m = Mutex.new
cv = ConditionVariable.new
Thread.new do
sleep(3) # A
m.synchronize{cv.signal}
end
m.synchronize{cv.wait(m)}
puts "Resource released." # B
This code makes the process commented as B
wait until A
finishes.
I understand the purpose of m.synchronize{...}
around cv.wait(m)
. What is the purpose of m.synchronize{...}
around cv.signal
? How would it be different if I had the following instead?
m = Mutex.new
cv = ConditionVariable.new
Thread.new do
sleep(3)
cv.signal
end
m.synchronize{cv.wait(m)}
puts "Resource released."