Created
November 14, 2009 07:52
-
-
Save eric/234436 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
| [9 : 0] eric@kiccoro:/Users/eric/src > ruby thread_sender.rb | |
| testing 1: #<Thread:0x101419bf8 run> | |
| testing 2: #<Thread:0x101419bf8 run> | |
| #<Thread:0x100170358 run> |
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 ThreadSender | |
| def initialize | |
| @queue = Queue.new | |
| @thread = Thread.new do | |
| loop do | |
| block, response_queue = *@queue.pop | |
| response_queue.push(block.call) | |
| end | |
| end | |
| end | |
| def thread_send(&block) | |
| response_queue = Queue.new | |
| @queue.push([block, response_queue]) | |
| response_queue.pop | |
| end | |
| end | |
| ts = ThreadSender.new | |
| ts.thread_send do | |
| puts "testing 1: #{Thread.current.inspect}" | |
| end | |
| ts.thread_send do | |
| puts "testing 2: #{Thread.current.inspect}" | |
| end | |
| puts Thread.current.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment