Atomicity of a simple assignment operator

C11 Standard says that for atomic types (_Atomic), the prefix and postfix operations ++ and -- are atomic (6.5.2.4., P2), as well as the compound assignments: op= (6.5.16.2, p3).

I did not write anything about the simple purpose of = . Is it also atomic?

Let say that E1, E2 int , but only E1 is determined by the _Atomic specifier. My guess is that this is:

 E1 = E2; 

is equivalent to:

 atomic_store( &E1 , E2 ); 

Is this my assumption right?

+5
source share
1 answer

Following the example of this Dr Dobbs article , the simple assignment of atomic variables in C11 is atomic .

Standard C11 (ISO / IEC 9899: 2011), section 6.2.6.1/9, states:

Loads and storages of objects with atomic types are performed using memory_order_seq_cst semantics.

In addition to atomic, operations performed with memory_order_seq_cst semantics have the same order observed by all threads (aka sequential sequential ordering ).

Without a classifier like _Atomic it is possible that the assignment will be non-atomic. Assigning a 64-bit value (for example, a long long ) on a 32-bit machine requires two CPU cycles. If another thread reads the value between the two loops, they will receive 4 bytes of the old value and 4 bytes of the new value.

+3
source

Source: https://habr.com/ru/post/1240951/


All Articles