I was reading about the at most once property that defines what an atomic action is and I’ve been curios about this example:
x = 0, y = 0
x = y + 1 || y = y + 1
If we use the ‘at most once’ definition, we can say that, indeed, x = y + 1
is atomic because
y
is only referenced in an another thread andx
is not being read by any thread
But the result of this action is not deterministic, it could be 1 or 2 depending on which operation is being computed first.
I might be wrong from the start, isn’t atomicity suppose to grant some kind of determinism?
1
What is the “at most once” property? If you’re discussing the definition of a term, I think it would be most useful to provide it.
As far as I understand it, an atomic action is supposed to guarantee that only two of the possible states are observable: either the action is carried out or it isn’t. The operation is not divisible. This is supported by the page over at Wikipedia.
It seems to me that you are making an assumption in your post that some arithmetic or logical operation is atomic, but under general conditions this assumption is false.
If you are sharing variables across multiple threads with no thread safety then you’re going to have a bad time when it comes to determinacy.
First up. Atomic actions are deterministic unto themselves.
x = y + 1, will always yield you the value of y +1, whatever y happens to be at the time of execution. At a system level, the result of this statement is a possible race condition due to y being updated in a different thread.
This isn’t technically a case of “non determinism”, as would be if you were to do
x = y + 1 * rand() (without using a set seed). This is more of a straight up race condition because you’re making assumptions about the state of variables that may or may not be correct.
The reality of this situation is “dont do that”. Dont use shared variables across threads. Copy input information into threads, copy information out. Always ensure that your threads have the data they need to do their work, and ensure they never need to reach outside their local threadspace to do it.
If you absolutely completely and utterly need to hit shared data, you must use synchronization primitives, but it makes things a hell of a lot simpler if you adopt a paradigm where threads do not hit shared memory except to deposit results.
And at that point you can use things like lockless queues and other wonderful bits and pieces to avoid the whole lock/unlock procedure.
For information purposes. if you absolutely must share y between threads. I would advise using a gate style write lock to block access to y while it is being written to by the other thread. all other times y will be able to be read immediately as the gate would be “open” to readers.
Yes and no.
Yes, an atomic action should always produce the same result given the same inputs.
No, a set of atomic actions, executed in parallel will not necessarily produce the same overall result. When steps are executed in parallel, the input(s) to a particular step may vary, in which case its output(s) may change accordingly.
Atomic actions are necessary but not sufficient to produce deterministic results.
3
There is no order guaranteed with multithreading, y = y + 1
may be run before or after x = y + 1
, which would change the result.
Atomicity avoids any “intermediate” state in a variable. For example, if y
is 64-bits long on a 32-bit system, setting it to a value is not naturally atomic, since the processor has to first set the lower 32 bits, then the upper ones. Reading y
from another thread in that case would mean that there’s a possibility that you’ll get a version of y
with only the lower 32 bits set.
Deterministic is not part of the definition of Atomic, in Computer Science. It is that simple. This is a good thing; separation of concerns is as important in CS definitions as in software design.
Why would you want to dictate that all atomic actions be deterministic? Very few applications can be entirely deterministic, and while it is good practice to isolate the non-deterministic functions, what would you gain by not allowing them to be atomic?
Atomic comes from the greek word “Atomos” which means indivisible.
In computer science this commonly refers to an operation which can only ever succeed or fail, or a datatype that can be fully read or written without failure.
In any sufficiently complex device, such as the realm of programming, there are any number of failures modes which aren’t accounted for (such as sudden power outages, RAM failure etc.)
This means that any kind of “Atomic” behaviour in modern computing is going to have multiple well defined limits when it comes to determinism. Modern CPUs purposely don’t always fully account for time due to caches etc. (we have interrupt timers for that) and this issue is magnified as race conditions when it comes to thread interaction (like in your example).
Many modern RDBs offer the practical illusion of Atomicity using transactions on SQL queries, these guarantees are only offered in so far, that, if it is possible to roll back the changes, the DB will do so. If any part of the system is disrupted, all bets are off.
Of course, it is possible to make hardware and software systems which may come closer to a pure definition of Atomic processes. Think of the “black box recorder” in Airplanes.