Skip to content

Instantly share code, notes, and snippets.

@slumos
Created February 21, 2013 01:28
Show Gist options
  • Select an option

  • Save slumos/5001184 to your computer and use it in GitHub Desktop.

Select an option

Save slumos/5001184 to your computer and use it in GitHub Desktop.
class ThreadThing
def initialize
@threads = []
@mutex = Mutex.new
@shutdown = false
trap('INT') { shutdown }
trap('TERM') { shutdown }
end
def thread
id = nil
@mutex.synchronize { id = @threads.length } # just being lazy...
@threads << Thread.new do
loop do
break if shutdown?
print "#{id}"
sleep 1
end
end
end
def shutdown?
@shutdown
end
def shutdown
@shutdown = true
end
def wait
@threads.first.join
end
end
puts $$
tt = ThreadThing.new
10.times { tt.thread }
tt.wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment