3 ms·
> The buffer[x].available = 0 will never run while buffer[x].available is equal to 0 overwriting or causing a data race when setting buffer[x].available to 1.
by loeg 3y ago
> The buffer[x].available = 0 will never run while buffer[x].available is equal to 0 overwriting or causing a data race when setting buffer[x].available to 1.
In particular, because loads and stores of the same variable cannot be reordered out of program order. Once your algorithm involves other variables, you would (likely) need to be a little careful about loading/storing with acquire/release semantics to prevent reordering other accesses relative to this protocol.
> Remember to use compiler memory barrier
I would highly recommend using the language atomic types (and barriers if truly needed) instead of gcc inline assembly syntax.
- samsquire 3y agoThanks for your reply. This subject is still new to me. My understanding of that syntax is that it is a compiler memory barrier, not a CPU memory barrier because the asm block is empty (no sfence or mfence).
- loeg 3y agoHey, no problem. > My understanding of that syntax is that it is a compiler memory barrier, not a CPU memory barrier because the asm block is empty (no sfence or mfence). In C11, you can write compiler-only fences with atomic_signal_fence: https://en.cppreference.com/w/c/atomic/atomic_signal_fence https://en.cppreference.com/w/c/atomic/atomic_signal_fence (In practice, though, I think it is rare that you actually want a compiler-only fence. Instead, correct use of acquire/release operations prevents reorderings.)
- samsquire 3y agoThank you loeg, I appreciate you and information you brought that TIL. I've been using a compiler fence to force reloads from memory to prevent -O3 from optimising away my variables/structs changing by other threads and keeping data in registers rather than reloading from memory each time. I saw the volatile recommended against from the Linux kernel programmers. such as my thread->running == 1 in my event loops for my threads. https://www.kernel.org/doc/html/latest/process/volatile-considered-harmful.html https://www.kernel.org/doc/html/latest/process/volatile-cons...
- loeg 3y ago> I've been using a compiler fence to force reloads from memory to prevent -O3 from optimising away my variables/structs changing by other threads I would highly recommend using the language standard atomic primitives instead.