lib/irrc/runner.rb in irrc-0.2.0 vs lib/irrc/runner.rb in irrc-0.2.1

- old
+ new

@@ -2,21 +2,24 @@ module Runner def run(threads) done = [] loop do - # NOTE: trick to avoid dead lock + # Trick to avoid dead lock if last_thread_of?(threads) && @queue.empty? terminate + + # Queue guard objects notifying other threads to return results logger.debug "Queue #{threads - 1} guard objects" (threads - 1).times { @queue.push nil } + return done end query = @queue.pop - # NOTE: trick to avoid dead lock + # Trick to avoid dead lock if query.nil? terminate return done end @@ -49,11 +52,11 @@ logger.debug %(Executing "#{command}") connection.cmd(command).tap {|result| logger.debug %(Got "#{result}") } end - def last_thread_of?(threads) - Thread.list.reject(&:stop?).size == 1 && Thread.list.size == threads+1 + def last_thread_of?(num_threads) + return @queue.num_waiting == num_threads - 1 end def terminate logger.info "No more queries" close