concurrency - Are there any operations/methods in Ruby that are guaranteed/documented to be atomic? -
i did quick google search, , written on atomicity in ruby suggest wrapping mutex around operation. however, suspect approach doesn't satisfy usual definition of atomicity, since signal interrupt synchronized code. example (taken ruby best practices):
lock = mutex.new # xxx example of not inside signal handler: trap(:usr1) lock.synchronize # if second sigusr1 arrives here, block of code # fire again. attempting mutex#synchronize twice # same thread leads deadlock error end end
i understand atomicity less important high level languages, sake of research canonical answer on matter implementations gil (e.g. mri 2.0.0) , without e.g. jruby 1.7.4, , rubinius 1.2.4
i have limited knowledge on topic. try answer best can.
there article jesse storimer wrote concurrency. highly recommend read of 3 parts it.
the conclusion on part 2 gil guaranteed native c method implementation atomic.
the example gave more of re-entrance problem atomicity. don't know if same thing or how close related is.
like article explain, ruby different event driven programming callback synchronous, meaning if send signal usr1 twice, second handler executed after first 1 finished. not lock on mutex twice.
but in signal handling in ruby asynchronous. meaning if send signal twice. second handler interrupt first handler. because first acquired lock, second handler try acquire same lock throw exception. , believe problem not ruby specific.
one of way of solving problem creating queue signal handling. solution using method called "self-pipe" trick. both of method. explained in article again awesome jesse storimer :
http://rubysource.com/the-self-pipe-trick-explained/
so, mri 2.0.0 believe still have gil, ruby guarantee native c method atomic.
jruby jvm backed guess of thread , locking mechanism implemented on top of jvm
rubinius 1.2 still have gil believe same mri. rubinius 2.x remove gil. haven't had experience rubinius i'm not entirely sure it.
and answer question, if working multi thread application in ruby. mutex classes should guard block executed single thread @ time.
Comments
Post a Comment