liveBook · Manning

Cnd_broadcast

As explained in Modern C, Third Edition

cnd_broadcast is a C threads-library function that wakes all threads waiting on a condition variable.

cnd_broadcast is a C condition-variable operation that wakes every thread currently waiting on the corresponding condition variable. The awakened threads return from cnd_wait or cnd_timedwait, reacquire the associated mutex, and must check the condition again before proceeding. section 20.5 section 21.2

Use cnd_broadcast with the same coordination rules as other condition-variable notifications:

  • Protect the shared state forming the condition expression with the mutex used by the waiters. section 20.5 section 21.2
  • Place the notification inside a critical section protected by that mutex when non-atomic shared data changes. section 21.2
  • Keep each wait inside a loop that tests both the condition and any termination state, because a wait can return before the condition is satisfied. section 20.5

Wake all waiters, then recheck

A waiter may use a loop such as while (!L->finished && (L->iteration <= L->drawn)) life_wait(&L->draw, &L->mtx);. A cnd_broadcast(&L->draw) notification wakes every thread waiting on L->draw, but each thread still reacquires L->mtx and evaluates the loop condition before accessing shared state. section 20.5 section 21.2

int cnd_wait(cnd_t*, mtx_t*);
int cnd_timedwait(cnd_t*restrict, mtx_t*restrict, const struct timespec 
 *restrict);
int cnd_signal(cnd_t*);
int cnd_broadcast(cnd_t*);

The condition-variable API listing places cnd_broadcast beside cnd_wait, cnd_timedwait, and cnd_signal, clarifying its role as the wake-all notification operation. section 20.5

How Modern C, Third Edition covers it

How deep it goes
Defines wake-all behavior, wait-loop discipline, and mutex-mediated visibility without presenting a dedicated broadcast implementation.
Examples
Uses a draw-thread wait loop and the C condition-variable declarations to illustrate notification and condition rechecking.
What it assumes
Assumes mutexes and condition-variable waits, then teaches wake-all notification, wait-loop rechecking, and mutex coupling.
What you can do after
Apply it via C condition-variable interfaces while preserving shared-state visibility through the waiters’ mutex.
How it connects
Connects condition variables with cnd_wait, cnd_timedwait, mutexes, critical sections, and release-acquire synchronization.

Also explained in

Compare how 2 books explain cnd_broadcast →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.