0

In my applications I have several threads that access and modify the same char array.

volatile unsigned char *volatile key[6] = { 0xD4 ,0x32 ,0xF8 ,0x0A ,0x10 ,0xE5 };

Will volatile unsigned char *volatile guarantee:

  1. Visibility (All other threads will see changes on the chars immediately)

  2. Atomicity (Any change will be executed by one single thread without interruption)

eztam
  • 3,443
  • 7
  • 36
  • 54
  • 2
    No. `volatile` only guarantees that every access in C code will translate into an actual read/write instruction to the memory (in the same order). – Eugene Sh. Nov 10 '21 at 17:25
  • You need to use `sig_atomic_t` to get an atomic type. – Barmar Nov 10 '21 at 17:26
  • Does this answer your question? [Concurrency: Atomic and volatile in C++11 memory model](https://stackoverflow.com/questions/8819095/concurrency-atomic-and-volatile-in-c11-memory-model) or https://stackoverflow.com/a/6397753/12939557 – Jérôme Richard Nov 10 '21 at 18:25
  • @JérômeRichard No, because this is about C++ and my question about C – eztam Nov 10 '21 at 18:40
  • @Barmar: AFAIK `sig_atomic_t` isn't atomic for the purposes of concurrent accesses by multiple threads, only for purposes of asynchronous access by a signal handler. You really need the `` types for the former. – Nate Eldredge Nov 10 '21 at 18:58
  • The second one is about C and not C++. There are also many similar post on SO already posted. AFAIK, the volatile keyword behave the same way in both C and C++ (and none of them guarantee visibility and atomicity). – Jérôme Richard Nov 10 '21 at 19:01
  • C has the same memory model here as C++. The short answers are NO and NO, and worse yet, concurrent access to this object is a data race causing undefined behavior. The `volatile` qualifier doesn't help with that. – Nate Eldredge Nov 10 '21 at 19:02
  • 1
    You seem to have `key` declared as an array of pointers, is that a typo? – Nate Eldredge Nov 10 '21 at 19:04
  • @JérômeRichard: On the other hand, the second one is also pre-C11 and thus not really up to date. I might go back and add an answer to it with some historical context. – Nate Eldredge Nov 10 '21 at 21:18
  • @EugeneSh. That's a common misconception, but it's not true. The C standard says what the instructions the compiler emits must *do*, not what those instructions must *be*. It is not possible for the C standard to say something like "memory operations can take place out of order so long as the compiler emits instructions that are in order" because the C standard describes what has to happen, not how the compiler has to cause it to happen. – David Schwartz Dec 25 '21 at 06:31

1 Answers1

2

Short Answer:

No

Long Answer:

From the GNU C Manual:

volatile tells the compiler that the variable is explicitly changeable, and seemingly useless accesses of the variable (for instance, via pointers) should not be optimized away. You might use volatile variables to store data that is updated via callback functions or signal handlers. Sequence Points and Signal Delivery.

This does not mean that variables with the volatile keyword necessarily will necessarily fulfill the conditions you asked.

If you want to have an array with both visibility and atomicity you should use mutexes, the specifics will of course depend on your goals.

Phoenix
  • 952
  • 2
  • 12