Created
July 29, 2014 21:46
-
-
Save buth/bc38eca3c118a23df400 to your computer and use it in GitHub Desktop.
Thread IO in Ruby
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
module ThreadIO | |
def ThreadIO.puts(s) | |
@@semaphore ||= Mutex.new | |
@@semaphore.synchronize do | |
STDOUT.puts s | |
end | |
Thread.pass | |
end | |
end | |
threads =[] | |
threads << Thread.new { 30.times { ThreadIO::puts "A" } } | |
threads << Thread.new { 30.times { ThreadIO::puts "B" } } | |
threads << Thread.new { 30.times { ThreadIO::puts "C" } } | |
threads.each do |thread| | |
thread.join | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment