Course contentsShow
← Computer Science
Lesson 1312 of 2,87230. Process SynchronizationFree lesson

Condition Variable Mesa vs Hoare Semantics

Understand the difference between Mesa (signal-and-continue) and Hoare (signal-and-wait) semantics.

Condition Variable Mesa vs Hoare Semantics

What You'll Learn

You'll understand the critical difference between two approaches to waking threads with condition variables: Mesa semantics (signal-and-continue) and Hoare semantics (signal-and-wait).

The Core Difference

When a thread signals a condition variable, what happens next? The answer depends on the semantics:

Hoare semantics (signal-and-wait): The signaling thread immediately blocks and transfers execution directly to the waiting thread. The signaled thread runs right away, guaranteed that the condition is still true when it wakes.

Mesa semantics (signal-and-continue): The signaling thread continues running. The signaled thread is merely moved to the ready queue and must compete for the CPU. By the time it actually runs, another thread might have changed the condition again.

Why It Matters

Think of it like a relay race:

  • Hoare: You hand the baton directly to the next runner, who starts immediately
  • Mesa: You drop the baton in a box and keep running; the next runner picks it up whenever they get scheduled

Most real systems (POSIX threads, Java, etc.) use Mesa semantics because it's simpler to implement and more efficient—no forced context switch. However, this is why you must use while loops instead of if statements when checking conditions:

// Mesa semantics requires while loop
while (condition_not_met) {
    pthread_cond_wait(&cv, &mutex);
}
// Re-check condition after waking!

With Hoare semantics, you could theoretically use if, but Mesa's flexibility means the condition must be rechecked.

Key Takeaway: Mesa semantics (signal-and-continue) lets the signaling thread keep running, requiring waiting threads to recheck conditions; Hoare semantics (signal-and-wait) guarantees immediate handoff but is less common in practice.