Max Max - 1 year ago 153
Ruby Question

Ruby Thread Pool issues

I'm using Ruby's

to manage ten threads for my program. I'm having two issues though:

  1. I want to enable abort_on_exception for the pool. My relevant code is:

    pool = Thread.pool(thread_count)
    pool.abort_on_exception = true

    However I'm getting

    undefined method abort_on_exception= for #<Thread::Pool:0x007fb25bb144b8> (NoMethodError)

    when I run it. I've triple-checked and
    is supposed to be an attribute in
    . I'm using threads version 0.2.2.

  2. I'm trying to use a
    mutex.synchronize {}
    block to let my threads access a public array one at a time, however when I run the program, nothing happens. It's as if it's getting stuck.

    pool = Thread.pool(thread_count)
    pool.process {
    mutex.synchronize do
    threadID = getOpenID

    puts "Starting #{threadID}"
    puts "Ending #{threadID}"

    mutex.synchronize do


Answer Source

The abort_on_exception method is defined on the Thread::Pool class itself, not on the individual instances. As such, you have to call

Thread::Pool.abort_on_exception = true

This method thus works very similar to Thread.abort_on_exception in the Ruby language core.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download