Let's see how to initialize a mutex. A mutex can be initialized in two ways. The first is to use the function pthread_mutex_init
and the second by a special macro: PTHREAD_MUTEX_INITIALIZER
. The initialization function receives a pointer to pthread_mutex_t
and a configuration for the mutex (pthread_mutexattr_t
). The initialization function returns 0 if it was possible to create the mutex.
#include <pthread.h>
//returns 0 in sucesses, other values diferent from 0 mean mistake
int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;