Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahemoff/ae386626923a1015a387 to your computer and use it in GitHub Desktop.
Save mahemoff/ae386626923a1015a387 to your computer and use it in GitHub Desktop.
Simple Ruby multi-threading demo
#!/usr/bin/env ruby
require 'thwait'
all = []
threads = []
5.times { |i|
threads << Thread.new {
sleep 2*rand
all << i
puts "Stored #{i}"
}
}
puts "before join #{all.inspect}" # empty
ThreadsWait.all_waits(*threads) # Old way - threads.each { |t| t.join }
puts "after join #{all.inspect}" # full
> ./go.rb
before join []
Stored 2
Stored 0
Stored 1
Stored 3
Stored 4
after join [2, 0, 1, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment