Reputation: 808
Okay, trivial question. Problem is, every answer I find on here has 3/4 conflicting answers.
I have a very simple problem. I have a global variable called ABORT_SIGNAL. As of now, this is declared volatile int ABORT_SIGNAL
. I understand that this does not do what I wish... which is...
I have two threads which may write to ABORT_SIGNAL. It will start at 0, and go to 1 after a number of seconds. Every other thread will be reading this variable on a regular basis, checking to see if the value has been set to 1.
Would the following achieve this ....
#include <stdint.h>
atomic_int ABORT_SIGNAL;
...
// When updating the value ...
atomic_store(&ABORT_SIGNAL, someValue);
// When reading the value ...
if (atomic_load_explicit(&ABORT_SIGNAL, memory_order_relaxed))
doSomething()
Others have also suggested that I would need to do something like the following.
After each write issue atomic_thread_fence(memory_order_acq_rel);
and before each read issue atomic_thread_fence(memory_order_acq_rel);
Upvotes: 0
Views: 246
Reputation: 78903
Using relaxed consistency makes not much sense if you want acquire semantics.
But, frankly, all of this looks much too complicated for not much gain.
Just use
_Bool _Atomic flag;
and then the usual ops, you don't need to use all theseatomic_...
pseudo functions. You get sequential consistency with that.
Then, you could investigate if this is really a bottleneck, and replace with another consistency at a specific place.
Upvotes: 2