# Thread Synchronization

- ## Locking and Unlocking Mutexes

  - int pthread_mutex_lock(pthread_mutex_t *mtx);
    - Thread blocks if mutex is already locked.
  - int pthread_mutex_trylock(pthread_mutex_t *mtx);
    - Returns EBUSY if mutex is already locked
  - int pthread_mutex_unlock(pthread_mutex_t *mtx);

- ## Spinlocks

  - To avoid blocking, continuously query via pthread_mutex_trylock()
  - Can be inefficient if mutex is held by another thread
  - Better to give up after a specified amount of time.
    - 1/2 the time of a thread context switch

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