-
-
Save frerich/4088669 to your computer and use it in GitHub Desktop.
This file contains 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
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) |
This file contains 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
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