Multi Threads writing to the same hash

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}

While the Ruby language has classes and methods for parallel execution,
the MRI and YARV Ruby interpreter (the standard old and new interpreters
of Ruby) cannot run threads parallel, in fact there is a mechanism that
blocks parallel execution, called global interpreter lock (GIL).
You get all functionality and side effects for threads (like race
conditions, deadlock, etc.) the just don’t run in parallel thus giving
you no speed bonus.

Install a different interpreter, like Rubinius (alias ‘rbx’), read the
documentation about Threads (as Rubinius might use them with
a bit different syntax) and test your code there.

If you are using RVM as Ruby environment, this should be no problem. If
you are not using RVM, well, you should :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs