I read this in the book C# 6.0 and the .NET 4.6 framework:
“assignments and simple arithmetic operations are not atomic”.
So, what does it exactly mean?
The ' |= ' symbol is the bitwise OR assignment operator.
In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...
C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.
In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more.
Atomic operations are ones that cannot be interrupted partway through, such as by threading. Take for instance the statement
_value++;
If you have two threads executing this code at once with a starting value of 0
, you may have the following
_value
, 0_value
, 0_value
, 1_value
, 1so now, even though we've called an increment twice, the final value in _value
is 1
, not the expected 2
. This is because increment operators are not atomic.
The function Interlocked.Increment
, however, is atomic, so replacing the above code with
Interlocked.Increment(ref _value);
Would solve the given race condition.
EDIT: As a point of etymology, "atomic" usually means "indivisible" - the chemistry term we're familiar with is a misnomer held over from the belief that atoms were indivisible, only for later discoveries to break them down further into subatomic, quark, and quanta levels.
An atom is indivisible. Atomic operations are "indivisible" operations, which cannot be divided, e.g., interrupted.
Microprocessors do not execute sequentially, that is, instruction after instruction, just like written in a program. There are external objects, which can change execution flow. A good example are interrupts.
So, you may know the MOV
instruction, which is available on pretty much all processors.
Imagine that it is executed by the CPU. a 32-bit value is moved into a 32-bit register.
Now, after 16 bits have been moved, an interrupt request occurs.
MOV
instruction will not stop but will execute to the end and then the CPU will handle the interruptMOV
instruction that is not atomic will immediately be stopped and the interrupt is executed. The problem is, if the interrupt accessed the register, which was written to by the MOV
, the content is unclear because the MOV
operation is only half-finished!Now, on usual processors, MOV
s operating on the processor's word size are atomic. If a processor word is 16 bits wide, a 16-bit MOV
instruction will be atomic.
However, a 32-bit MOV
operation would not be atomic. Such a non-atomic MOV
instruction is normally not provided by the instruction set but by some higher-level language as with C's long long
or C#'s long
. Operations on these data types are not guaranteed to be atomic!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With