Skip to content

Instantly share code, notes, and snippets.

@SamSaffron
Created May 12, 2017 00:13
Show Gist options
  • Save SamSaffron/57018ae8bf8fbf6353d23714baf4633a to your computer and use it in GitHub Desktop.
Save SamSaffron/57018ae8bf8fbf6353d23714baf4633a to your computer and use it in GitHub Desktop.
require 'thread'
mutex = Mutex.new
10000.times do |i|
r,w = IO.pipe
t = Thread.new {
begin
loop do
c = r.getc
break if c == '!'
end
# you need this, so you don't shut down the socket prior to print being fully handled
mutex.synchronize do
r.close
w.close
end
rescue => e
p e
end
}
mutex.synchronize do
w.print '!'
end
t.join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment