Bounded Queue in Ruby

I don’t post code on here nearly as often as I should. Here’s some code I just finished for a future RubyTapas episode. It implements a thread-safe, optionally bounded, timeout-enabled queue class suitable for communication between threads. Please throw rocks at it and tell me what I got wrong. It’s a Gist, so feel free to submit alternatives via forks.


[gist id=6442810]

EDIT: Eric Hodel has pointed out one issue already, via Twitter: I’d forgotten to signal @space_available after popping an item off of the queue!


    1. Correct. Ruby’s queue classes don’t support timeouts, which is a pretty big omission in multithreaded primitives. Especially given that pre-2.0, the Timeout module cannot be used safely with queues (or anything, really).

Leave a Reply

Your email address will not be published. Required fields are marked *