Ruby是否具有原子变量,如Java中的AtomicInteger或AtomicBoolean?
解决方法
Here is a gem可能提供您需要的(发现
linked from here).
The code is clean and compact enough to quickly understand(它基本上是一个互斥体,正如其他人所建议的),如果你想编写自己的Mutex包装器,这应该给你一个很好的起点.
require 'atomic' my_atomic = Atomic.new('') # set method 1: my_atomic.update { |v| v + 'hello' } # set method 2: begin my_atomic.try_update { |v| v + 'world' } rescue Atomic::ConcurrentUpdateError => cue # deal with it (retry,propagate,etc) end # access with: puts my_atomic.value