簡単な Google 検索を行ったところ、Ruby のアトミック性について書かれているほとんどすべてが、操作を Mutex でラップすることを提案しています。ただし、シグナルが同期されたコードを中断する可能性があるため、このアプローチはアトミック性の通常の定義を満たさないのではないかと思います。例 ( Ruby Best Practicesから引用):
lock = Mutex.new
# XXX this is an example of what NOT to do inside a signal handler:
trap(:USR1) do
lock.synchronize do
# if a second SIGUSR1 arrives here, this block of code
# will fire again. Attempting Mutex#synchronize twice
# the same thread leads to a deadlock error
end
end
高水準言語では原子性がそれほど重要ではないことは理解していますが、研究のために、GIL ( MRI 2.0.0 など) を使用した実装と、JRuby 1.7.4 や Rubinius を使用しない実装 について、この問題に関する標準的な回答を得たいと思います。 1.2.4