Created
March 18, 2017 01:19
-
-
Save jwhiteman/c86a713c85e5cb23e1098b1de8f81550 to your computer and use it in GitHub Desktop.
ruby condition variable and broadcast
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
m = Mutex.new | |
c = ConditionVariable.new | |
tg = ThreadGroup.new | |
t = 5.times.map do |n| | |
tg.add(Thread.new do | |
print "starting #{n}\n" | |
m.synchronize do | |
c.wait(m) | |
end | |
print "#{n} done!\n" | |
end) | |
end | |
tg.list.count | |
# => 5 | |
c.broadcast | |
tg.list.count | |
# => 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment