I’ve been reading and learning about the C++ memory model and relaxed atomics and decided to rewrite my Interview question, part 1 using atomic<bool>’s to synchronize the threads. I took it one step further and created three threads that print A, B, C, A, B, C,…
Each thread waits for its atomic<bool> to be set to true while spinning; prints then sets the next thread’s flag. The wait is a CAS (compare-and-swap) operation.
Next, I will try to figure out how to relax the compare_exchange and the store calls with memory_order_acquire and memory_order_release parameters 🙂

Complete listing:

A
B
C
A
B
C
A
B
C
Program ended with exit code: 1

Program output.

Leave a Reply