liveBook · Manning

Cnd_timedwait

As explained in Modern C

cnd_timedwait is a C standard threads function that atomically releases a mutex while waiting on a condition variable until notification or an absolute timeout, then reacquires the mutex.

cnd_timedwait waits on a condition variable with a mutex and an absolute time limit. While waiting, the thread is paused; the mutex is associated with the condition variable until the waiter reacquires it. section 18.5

One-second bounded wait

A helper obtains the current UTC time, adds one second to now.tv_sec, and passes that absolute deadline to cnd_timedwait(cnd, mtx, &now). The caller therefore pauses until notification or timeout.

Use the wait as part of a guarded condition loop:

  • After return, check the predicate again: notification does not guarantee that the condition expression changed, and timeout can also return. section 18.5

How Modern C covers it

How deep it goes
Explains arguments, absolute deadlines, mutex association, and predicate rechecking, but defers implementation internals.
Examples
Uses a one-second life_wait helper and accounting, update, and drawing thread predicates.
What it assumes
Assumes mutexes, condition variables, shared predicates, and thread coordination; demonstrates predicate rechecking explicitly.
What you can do after
Apply it via C threads by supplying a condition variable, locked mutex, and absolute timespec deadline inside a predicate loop.
How it connects
Connects condition variables with mutexes, threads, cnd_wait, cnd_signal, and cnd_broadcast.

Also explained in

Compare how 2 books explain cnd_timedwait →

Related concepts

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