Skip to content

Instantly share code, notes, and snippets.

@buth
Created July 29, 2014 21:46
Show Gist options
  • Save buth/bc38eca3c118a23df400 to your computer and use it in GitHub Desktop.
Save buth/bc38eca3c118a23df400 to your computer and use it in GitHub Desktop.
Thread IO in Ruby
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