Created
July 11, 2013 17:00
-
-
Save timabdulla/5977231 to your computer and use it in GitHub Desktop.
demonstrating the behaviour of Mutex#sleep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'thread' | |
mutex = Mutex.new | |
t1 = Thread.new do | |
mutex.synchronize do | |
mutex.sleep | |
# we've now been woken up, and we'll try and reacquire the mutex | |
puts "woken up - mutex locked: #{mutex.locked?}" | |
end | |
end | |
t2 = Thread.new do | |
loop { break if t1.status == 'sleep' } | |
# t1 is now sleeping on the mutex, so the mutex will be unlocked | |
puts "t1 is sleeping - mutex locked: #{mutex.locked?}" | |
# let's wake up t1 | |
t1.run | |
end | |
[t1, t2].map(&:join) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment