Presuming that ThreadA()
and ThreadB()
are actually executed concurrently in the first place, then the C++ standard would indeed theoretically guarantee that ThreadB()
will eventially read the change to the atomic and terminate properly.
In reality, things can get a bit more messy. I can think of three scenarios where ThreadB()
will never see the change to the atomic:
Something outside of threads A and B terminates thread B prematurely, or stalls it indefinitely. Then obviously it can't get to the point where it sees the change.
Something outside of threads A and B terminates thread A prematurely, or stalls it indefinitely. Then obviously it can't get its message out, and thread B will never see it.
Something outside of threads A and B disrupts communication between whatever hardware entities are running the respective threads.
3 is, fortunately, a rather hypothetical scenario for pieces of software, but in e.g. a supercomputer environment, I can imagine that it may be worth hardening software against such scenarios if they cannot be robustly handled at a hardware level.
1 and 2 may actually happen even on single-core machines. For instance, the user might pause the program using a debugger, suspend one thread, unpause the others, and then walk away from the keyboard to buy a pack of cigarettes, never to return. Or maybe some anti-virus software goes haywire and decides that thread A is a security threat, and that it needs to be terminated, but for some obscure reason does not terminate thread B.
This may sound trivial, but in a more problematic variant of scenario 2, maybe something in your program causes the operating system to run low on resources until thread B completes. Then the lack of resources could potentially cause the operating system to stall thread A because it thinks it has more important tasks to throw its sparse resources at, which in turn would prevent the operating system to recover.
(A similar thing might conceivably happen with scenario 1, though it is a bit more boring.)
None of these scenarios are sensitive to the memory order though.