Skip to content

Instantly share code, notes, and snippets.

@timabdulla
Created July 11, 2013 17:00
Show Gist options
  • Save timabdulla/5977231 to your computer and use it in GitHub Desktop.
Save timabdulla/5977231 to your computer and use it in GitHub Desktop.
demonstrating the behaviour of Mutex#sleep
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