Skip to content

Instantly share code, notes, and snippets.

@eric
Created November 14, 2009 07:52
Show Gist options
  • Select an option

  • Save eric/234436 to your computer and use it in GitHub Desktop.

Select an option

Save eric/234436 to your computer and use it in GitHub Desktop.
[9 : 0] eric@kiccoro:/Users/eric/src > ruby thread_sender.rb
testing 1: #<Thread:0x101419bf8 run>
testing 2: #<Thread:0x101419bf8 run>
#<Thread:0x100170358 run>
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