Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write an atomic operation

I would like to execute some methods atomicity with Ruby, according to http://en.wikipedia.org/wiki/Atomicity_(database_systems)

For instance, if I have:

a = 30
b = 75

I would like to be able to do something like:

atomic_operation do
  a += 10
  b -= 39
end

Is there a native module in Ruby 1.9 that allow such process? If possible, I would like to do so without using a plugin. Many thanks!

like image 792
moshimoshi Avatar asked Jul 07 '10 22:07

moshimoshi


People also ask

What are atomic write operations?

For example, an atomic read / write operation. Or atomic access to a property. But what does this mean? Generally, you can summarize atomic as "one at a time". For example, when accessing or mutating a property is atomic, it means that only one read or write operation can be performed at a time.

What is atomic operation C++?

(C++11) [edit] The atomic library provides components for fine-grained atomic operations allowing for lockless concurrent programming. Each atomic operation is indivisible with regards to any other atomic operation that involves the same object. Atomic objects are free of data races.

What is an atomic operation in Java?

Atomic Operations. Those operations that always execute together is known as the atomic operations or atomic action. All the atomic operations either execute effectively happens all at once or it does not happen at all. Three key concepts are associated with atomic actions in Java are as follows: 1.

Is ++ an atomic operation?

On objects without an atomic type, standard never defines ++ as an atomic operation.


1 Answers

It really depends on the scope you are interested in as to the right tools for the job. If you are looking to perform an atomic operation on a database, then the database driver will probably (if it's any good/the database supports it) offer a way to use a database transaction to make updates atomic.

If you are talking about a multi-threaded Ruby application attempting to makes updates to shared resources atomic and thread-safe, then Ruby provides the Mutex and ConditionVariable classes to help you out in that regard. (More info: http://ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html)

like image 85
Mike Tunnicliffe Avatar answered Dec 21 '22 23:12

Mike Tunnicliffe