Sha256: 5ff20617d251bf6cacf88247ec34a23ab33dd6ea46470d900a5b0cf3a8e651bf

Contents?: true

Size: 874 Bytes

Versions: 1

Compression:

Stored size: 874 Bytes

Contents

$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)

require 'hyper_thread'

# make a pool of maximum 7 threads
pool = HyperThread.pool.new(max: 10)

# spawn 7 threads, and 3 jobs will be added to the queue
pool.async(count: 10) do
  puts "some work to do"
end

# spool off queued jobs
pool.todo(count: pool.queue.size) do |block|
  block.call # "some work to do"
end

# kill off any dead threads
pool.reap

# queue up 300 jobs
counter = 0

pool.qsync(count: 300) do
  puts "some work to do #{counter += 1}"
end

# spawn 2 threads to manage the queue forever
pool.async(forever: true, count: 2) do
  pool.todo do |block|
    block.call
  end
end

pool.qsync(count: 300) do
  "some work to do #{counter += 1}"
end

# let them manage that queue for roughly 2 seconds
sleep 2

# shutdown the thread pool
puts "Shutting down #{pool.threads.count} threads."
pool.shutdown

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hyper_thread-1.0.0 examples/readme_example.rb