Created
February 21, 2013 01:28
-
-
Save slumos/5001184 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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