Skip to content

Day59 - pthread Condition Variable Synchronization

Date

2026-05-25

Goal

Learn condition synchronization in Linux userspace.

Topics:

  • pthread_create / pthread_join
  • pthread_cond_init
  • pthread_cond_wait
  • pthread_cond_signal
  • pthread_cond_broadcast
  • pthread_cond_timedwait
  • predicate loop
  • lost wakeup
  • spurious wakeup
  • producer consumer synchronization

Completed

Minimal condition variable demo

Implemented:

  • worker wait
  • main signal
  • shared predicate

Observed:

  • cond_wait releases mutex internally
  • wakeup reacquires mutex

Producer Consumer

Implemented:

  • bounded queue
  • queue full wait
  • queue empty wait
  • producer signal
  • consumer signal

Compared with semaphore queue.


Timeout synchronization

Implemented:

  • pthread_cond_timedwait()

Observed:

  • timeout is not an error
  • timeout does not imply condition satisfied

Multi-consumer

Implemented:

  • two consumers
  • signal
  • broadcast

Observed:

  • wakeup does not guarantee progress
  • predicate must always be re-checked

Key Learning

Condition variable:

wait for state

Semaphore:

wait for resource

Condition variable itself does not store state.

Shared state remains source of truth.


Next

Day60

Linux synchronization internals:

  • atomics
  • memory ordering
  • futex