I am working on a project that deals with lots of atomic operations. Till now I didn’t knew about atomic_load()
and was only relying on assignment operator to get value of an atomic type and I haven’t seen an error except of so much of testing. Those atomic types are changed by multiple processes and threads as well by atomic_compare_exchange_strong_explicit()
, so they will need an old value every time, and that’s where I always did oldValue = <Atomic_ type_variable>
and it always works fine.
Is that just by chance? Should I prefer using atomic_load()?

- 8,038
- 2
- 40
- 58

- 6,059
- 3
- 14
- 39
-
What ordering do you need? – curiousguy May 24 '19 at 14:19
-
I am not really familiar wit memory orderings(really confusing). Maybe that’s in atomic_load_explicit(). My question was regarding just atomic_load(), for which someone gave the answer. – Mihir Luthra May 24 '19 at 18:03
-
You are confused? So his everybody, I believe. There are arguments by researchers that the specification of relaxed ordering is defective in a deep way! – curiousguy May 24 '19 at 18:06
1 Answers
foo = atomic_var
is just a shortcut syntax for foo = atomic_load(&atomic_var);
Which itself is a shortcut for foo = atomic_load_explicit(&atomic_var, memory_order_seq_cst);
That has a use-case when you want to use an ordering weaker than the default seq_cst
.
The main reason for using atomic_load
explicitly in your source code is probably to remind human readers that a variable or pointer is atomic. Or maybe as a part of a macro, using atomic_load(&(macro_input))
would create a compile-time error for a non-atomic pointer.
As a "generic" function, you can't take a normal function-pointer to it.
Its existence may be just to make it easier to write the language standard, and explain everything in terms of functions.
It's not the actual assignment that's key here, it's evaluating the atomic variable in an rvalue context (reading it's value as part of an expression, like you typically find on the right-hand side of an =
). printf("%d\n", my_atomic_var);
is also equivalent to atomic_load
.
And BTW, the same thing holds for atomic_var = foo;
being exactly the same as atomic_store_explicit
with mo_seq_cst
. Here it is assignment that's key.
Other kinds of lvalue references to an atomic variable are different, like read-modify-write atomic_var++
is equivalent to atomic_fetch_add
.

- 328,167
- 45
- 605
- 847