UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

11.3.5.  Mutex: Lock

The following functions may be used to lock a mutex:

#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_timedlock (pthread_mutex_t *mutex, 
  const struct timespec *restrict abs_timeout);

When locking a mutex the first option available to the user is pthread_mutex_lock, which has the following behavior on the thread:

  • If the mutex is not taken, the mutex is taken and the thread continues with its execution.

  • If the mutex lock is taken, the thread is blocked interrupting the thread execution.

The try_lock variant catches the mutex until it ends and with timedlock awaiting a maximum time.