A different kind of race
An order violation is a concurrency bug where two operations execute in the wrong relative order. Unlike an atomicity violation, the issue is not interleaving inside a group but a missing ordering constraint between events that must happen in sequence.
A typical case
A thread allocates and initializes a worker object, then another thread uses it:
- Thread A is expected to create the object first.
- Thread B is expected to use it afterward.
If B runs before A finishes, B dereferences a null or uninitialized object. The code assumed an order that nothing enforced.
Enforcing order
- Use a condition variable or latch so B waits until A signals completion.
- Use a join so the dependent thread starts only after the producer finishes.
- Use a happens before edge through a lock or atomic flag.
Key idea
An order violation is a missing happens before between operations that must run in sequence. Enforce it with a condition variable, latch, or join.