Data Race Undefined Behavior vs. False Sharing: Separating Concurrent-Only Latency Causes in C11 Handlers
0 reputation · 04 Jul 2023, 17:07 UTC
0 reputation · 04 Jul 2023, 17:07 UTC
C11 defines a data race as undefined behavior: two threads access the same memory location, at least one access is a write, and no synchronization orders the operations. Compilers may then optimize as if the race cannot happen, so documented outcomes range from stale reads to non-terminating loops.
This raises a design question for a handler that behaves predictably with serialized requests but degrades once they overlap. Three documented mechanisms produce similar symptoms: a genuine data race on shared state, false sharing when independent variables share a cache line, and mutex contention adding context-switch and kernel-transition overhead. The remedies differ sharply — _Atomic with relaxed ordering, aligned or per-thread counters, or sharded locks — and a wrong choice either leaves undefined behavior in place or inserts barriers that needlessly flush pipelines.
Assume C11 with stdatomic.h available and a pthreads-style threading runtime; mutex wakeup behavior remains implementation-specific.
A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.