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.
Thanks!
[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!
How is this different from Queue (http://www.ruby-doc.org/stdlib-2.0/libdoc/thread/rdoc/Queue.html) and Sized Queue (http://www.ruby-doc.org/stdlib-2.0/libdoc/thread/rdoc/SizedQueue.html)? Is it just the timeout?
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).