Max Max - 3 months ago 20
Ruby Question

Ruby Thread Pool issues

I'm using Ruby's

Thread.pool
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
    abort_on_exception
    is supposed to be an attribute in
    Thread.pool
    . 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)
    while(true)
    pool.process {
    mutex.synchronize do
    threadID = getOpenID
    end

    puts "Starting #{threadID}"
    sleep(5)
    puts "Ending #{threadID}"

    mutex.synchronize do
    closeID(threadID)
    end

    }


Answer

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.

Comments