require 'monitor' class CountingSemaphore def initialize(count) @count = count @monitor = Monitor.new @zero_cond = @monitor.new_cond end def synchronize @monitor.synchronize do @zero_cond.wait_while{ @count == 0 } @count -= 1 end ret = yield @monitor.synchronize do @count += 1 @zero_cond.broadcast end ret end end cs = CountingSemaphore.new(10) Array.new(100) do Thread.start do cs.synchronize{ `sle