pthread_mutex_init(),
pthread_mutex_lock() (and unlock)
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
Example:
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
// or: pthread_mutex_t lock; assert( pthread_mutex_init(&lock,NULL) == 0);
...
pthread_mutex_lock(&lock);
x = x + 1; // or whatever your critical section is
...
pthread_mutex_unlock(&lock);