Skip to content

Instantly share code, notes, and snippets.

@jwhiteman
Created March 18, 2017 01:19
Show Gist options
  • Save jwhiteman/c86a713c85e5cb23e1098b1de8f81550 to your computer and use it in GitHub Desktop.
Save jwhiteman/c86a713c85e5cb23e1098b1de8f81550 to your computer and use it in GitHub Desktop.
ruby condition variable and broadcast
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