Created
February 5, 2014 21:11
-
-
Save JoshMcKin/8833189 to your computer and use it in GitHub Desktop.
Stack Queue; Last in first out Queue
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
require 'thread' | |
# Borrowed from https://github.com/ruby/ruby/blob/ruby_1_9_3/lib/thread.rb#L140 | |
# and modified for a last in first out processing | |
# | |
# | |
class StackQueue < Queue | |
# Last in first out Queue | |
# | |
# Retrieves data from the queue. If the queue is empty, the calling thread is | |
# suspended until data is pushed onto the queue. If +non_block+ is true, the | |
# thread isn't suspended, and an exception is raised. | |
# | |
def pop(non_block=false) | |
@mutex.synchronize{ | |
while true | |
if @que.empty? | |
raise ThreadError, "queue empty" if non_block | |
@waiting.push Thread.current | |
@mutex.sleep | |
else | |
return @que.pop # was shift | |
end | |
end | |
} | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment