Hi,
I have many process that are can run in parallel.
And I prefer/need to store their result in a hash.
There is no worry of data corruption since each process writes to a
unique hash key.
But from the performance I’m getting I suspect that I need to improve my
access synchronization to the hash.
When I tried using Mutex it actually made it much slower (I think it
locks up the hash and won’t let other process write to it).
The sample code of what I have now is written below.
Thank you,
Ohad.
==============================================================
def foo_sleep()
i = rand(100)
sleep(i)
return i
end
thread_limit = 10
threads = []
result = Hash.new()
(1…1000).do |i|
Controlling the number of thread working in parallel:
while threads.size > threads_limit
sleep 0.1
threads.delete_if{ |thr| thr.status == false or thr.status == nil
}
end
threads.push(Thread.new {
result[i] = foo_sleep()
})
end
threads.each {|t| t.join}