← Lessons

quiz vs the machine

Gold1410

Concurrency

The Order Violation

Operation B runs before operation A, even though A must come first.

4 min read · core · beat Gold to climb

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.

Check yourself

Answer to earn rating on the learn ladder.

1. How does an order violation differ from an atomicity violation?

2. Which primitive enforces that a consumer runs after a producer?