Last active
August 29, 2015 14:14
-
-
Save mahemoff/ae386626923a1015a387 to your computer and use it in GitHub Desktop.
Simple Ruby multi-threading demo
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
#!/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 |
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
> ./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