Skip to content

Instantly share code, notes, and snippets.

@mperham
Created April 11, 2011 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mperham/913798 to your computer and use it in GitHub Desktop.
Save mperham/913798 to your computer and use it in GitHub Desktop.
require 'thread'
class Queue
def timed_pop(timeout=0.5)
@mutex.synchronize {
while true
if @que.empty?
@waiting.push Thread.current
raise Timeout::Error if @mutex.sleep(timeout) != 0
else
return @que.shift
end
end
}
end
end
q = Queue.new
Thread.new { sleep 0.1; q << 'hello!' }
puts q.timed_pop(0.5)
ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.6.0]
~> ruby foo.rb
hello!
~> rvm use jruby
Using /Users/mperham/.rvm/gems/jruby-1.6.0
~> jruby -v
jruby 1.6.0 (ruby 1.8.7 patchlevel 330) (2011-03-15 f3b6154) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_24) [darwin-x86_64-java]
~> jruby foo.rb
NoMethodError: undefined method `synchronize' for nil:NilClass
timed_pop at foo.rb:5
(root) at foo.rb:20
1 ~> jruby --1.9 foo.rb
NoMethodError: undefined method `synchronize' for nil:NilClass
timed_pop at foo.rb:5
(root) at foo.rb:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment