# Thread Synchronization

- ## Condition Variables - produce true suspended wait

  - 3 parts: variable, mutex, and predicate

- ## Atrributes, Initialization, and Destruction (PC == pthread_cond below)

  - int pthread_condattr_init(pthread_condattr_t * attr);
  - int pthread_condattr_destroy(pthread_condattr_t * attr);
  - int pthread_cond_init(pthread_cond *cond, PCattr_t *attr);
  - int pthread_cond_destroy(PC_t *cond, PCattr_t*attr);
  - pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

- ## Waiting on a condition

  - Mutex must be acquired first (PM_T == pthread_mutex below)
  - int pthread_cond_wait(PC_t *cond, pthread_mutex_t *mutex);
  - int PC_timedwait(PC_t *cond, PM_t *mutex, struct timespec *abstime)
  - wait() does the following implicitly:
    - Unlock mutex, block, get signal, lock mutex, return
  - Application should explicitly check predicate after returning from wait()

Previous slide | Next slide | Back to first slide | View graphic version