Atomicness for a global variable shared by threads


Atomicness for a global variable shared by threads



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...


volatile int ABORT_SIGNAL



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);


atomic_thread_fence(memory_order_acq_rel);


atomic_thread_fence(memory_order_acq_rel);





Since in the end the value will be 1, why do you need atomicness ? Whatever thread is writing, and even if they are writing together, you'll never have a incoherent value : Just 0, and then 1.
– Joël Hecht
Jul 2 at 5:10






So so you think this is a case where volatile will get the job done by itself?
– AndrewGrant
Jul 2 at 5:15





Yes, I think so, volatile will prevent optimizations, forcing the access to the variable each time it's used which is important for variables shared between threads (this is not atomicness).
– Joël Hecht
Jul 2 at 6:36


volatile





@JoëlHecht important for variables shared between threads You're misusing volatile if you're using it to share data between threads. Read this: Volatile: Almost Useless for Multi-Threaded Programming
– Andrew Henle
Jul 2 at 9:53



volatile





@AndrewHenle I followed the link you gave and read the article. Since it make me doubt of what I thought, I made a case test. The conclusion is unambiguous : if you turn on optimizations when compiling a multithread program, volatile is not useless at all. In my simple case test, removing volatile ends up in an infinite loop. But the article is not false, it's just that the two cases it treats are not all cases. And here, according the variable name ABORT_SIGNAL, I guess we are in a third case.
– Joël Hecht
Jul 2 at 19:15


volatile


volatile


ABORT_SIGNAL




1 Answer
1



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.


atomic_...



Then, you could investigate if this is really a bottleneck, and replace with another consistency at a specific place.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages