I've asked the question in stack overflow, but no luck there. I hope I will see some light here.
I'd like to write a C++ object where there are many logger threads logging to a large global (non-atomic) ring buffer, with an occasional reader thread which wants to read as much data in the buffer as possible. I ended up having a global atomic counter where loggers get locations to write to, and each logger increments the counter atomically before writing. The reader tries to read the buffer and per-logger local (atomic) variable to know whether particular buffer entries are busy being written by some logger, so as to avoid using them.
So I have to do synchronization between a pure reader thread and many writer threads. I sense that the problem can be solved without using locks, and I can rely on "happens after" relation to determine whether my program is correct.
I've tried relaxed atomic operation, but it won't work: atomic variable stores are releases and loads are acquires, and the guarantee is that some acquire (and its subsequent work) always "happens after" some release (and its preceding work). That means there is no way for the reader thread (doing no store at all) to guarantee that something "happens after" the time it reads the buffer, which means I don't know whether some logger has overwritten part of the buffer when the thread is reading it.
So I turned to sequential consistency. For me, "atomic" means Boost.Atomic, which notion of sequential consistency has a "pattern" documented:
The third pattern for coordinating threads via Boost.Atomic uses seq_cst for coordination: If ...
- thread1 performs an operation A,
- thread1 subsequently performs any operation with seq_cst,
- thread1 subsequently performs an operation B,
- thread2 performs an operation C,
- thread2 subsequently performs any operation with seq_cst,
- thread2 subsequently performs an operation D,
then either "A happens-before D" or "C happens-before B" holds.
Note that the second and fifth lines say "any operation", without saying whether it modify anything, or what it operates on. This provides the guarantee that I wanted.
All is happy until I watch the talk of Herb Sutter titled "atomic<> Weapons". What he implied is that seq_cst is just a acq_rel, with the additional guarantee of consistent atomic stores ordering. The suggestion that sequentially consistent atomic operations may be re-ordered with non-atomic operations only adds to my mystery. I turned to the cppreference.com, which have similar descriptions.
So my questions: