I have a situation with one writer, with several readers. There is a counter on which one thread writes, and any thread can read this counter. Since you don’t have to worry about fighting with other streams to access data for one recording stream, is the following code safe?
#include <stdatomic.h>
#include <stdint.h>
_Atomic uint32_t counter;
uint32_t increment_counter() {
atomic_fetch_add_explicit(&counter, 1, memory_order_relaxed);
return counter;
}
uint32_t load_counter() {
return atomic_load_explicit(&counter, memory_order_relaxed);
}
The writer thread simply reads counterdirectly without calling any function atomic_load*. This should be safe (since you need to read a value to read multiple lines), but I don’t know if the variable declaration forbids _Atomicusing this variable directly or if you should always read it using one function atomic_load*.