Mutexes and Synchronization

pthread_mutex_lock() / pthread_mutex_unlock() protect critical sections. Deadlock occurs when two threads each hold a lock the other needs — avoid by always acquiring locks in the same order. Condition variables (pthread_cond_wait / pthread_cond_signal) let a thread sleep until a condition is met, atomically releasing and re-acquiring the mutex. Always check the condition in a while loop (spurious wakeups). Reader-writer locks (pthread_rwlock_t) allow concurrent reads but exclusive writes.

Appears In

m09-concurrency