Alexandre
Alexandre

Reputation: 5135

How to efficiently supervise Ruby Threads?

Is the following inefficient? I want to allocate nearly all resources to threads but I'm wondering if in this case this loop will consume a lot of CPU time.

Thanks!

threads = create_threads #method that returns an Array of Threads
loop do
  alive = false
  threads.each do |thread|
    if thread.alive?
      alive = true
    end
  end
  break unless alive
end

Upvotes: 2

Views: 909

Answers (2)

Kay Sarraute
Kay Sarraute

Reputation: 1242

threads.each &:join

my_thread.join returns as soon as my_thread exits.

Upvotes: 3

Farrel
Farrel

Reputation: 2381

threads.each do |thread|
  thread.join
end

Upvotes: 2

Related Questions