How can I return a value from a thread in Ruby?

后端 未结 5 1101
清歌不尽
清歌不尽 2021-02-03 19:08

If I have the following code :

threads = []
(1..5).each do |i|
  threads << Thread.new { `process x#{i}.bin` } 
end
threads.each do |t|
  t.join
  # i\'d l         


        
5条回答
  •  不思量自难忘°
    2021-02-03 19:58

    You should use the Queue class. Each thread should put its result in the queue, and the main thread should fetch it from there. Notice that using that approach, results my be in a order different from thread creation order in the queue.

提交回复
热议问题