ruby - Thread and Queue -


i interested in knowing best way implement thread based queue.

for example:

i have 10 actions want execute 4 threads. create queue 10 actions placed linearly , start first 4 action 4 threads, once 1 of thread done executing, next 1 start etc - @ time, number of thread either 4 or less 4.

there queue class in thread in standard library. using can this:

require 'thread'  queue = queue.new threads = []  # add work queue queue << work_unit  4.times   threads << thread.new     # loop until there no more things     until queue.empty?       # pop non-blocking flag set, raises       # exception if queue empty, in case       # work_unit set nil       work_unit = queue.pop(true) rescue nil       if work_unit         # work       end     end     # when there no more work, thread stop   end end  # wait until threads have completed processing threads.each { |t| t.join } 

the reason pop non-blocking flag between until queue.empty? , pop thread may have pop'ed queue, unless non-blocking flag set stuck @ line forever.

if you're using mri, default ruby interpreter, bear in mind threads not absolutely concurrent. if work cpu bound may run single threaded. if have operation blocks on io may parallelism, ymmv. alternatively, can use interpreter allows full concurrency, such jruby or rubinius.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -