1. Mutex: Mutex
A. For mutex Access B. Type: pthread_mutex_t, must be initialized to Pthread_mutex_initializer (for statically assigned mutex, equivalent to Pthread_mutex_init (..., NULL) ) or call Pthread_mutex_init. Mutexes should also be destroyed with Pthread_mutex_destroy. #i nclude <pthread.h>1) int pthread_mutex_init ( pthread_mutex_t * Restrict mutex, const pthread_mutexattr_t *restrict attr); function pthread_mutex_ Init is used to generate a mutex, the null parameter indicates that the default property is used, and if a mutex for a particular property needs to be declared, the function Pthread_mutexattr_init must be called. Function pthread_mutexattr_setpshared and functions The Pthread_mutexattr_settype is used to set the Mutex property. The previous function sets the property pshared, which has two values, pthread_process_private, and pthread_process_shared. The former is used for thread synchronization in different processes, and the latter is used to synchronize different threads of the process. 2) int Pthread_mutex_destroy (pthread_mutex_t *mutex); 3) int pthread_mutex_lock (pthread_mutex_t *mutex); 4) int Pthread_mutex_trylock (pthread_mutex_t *mutex); 5) int pthread_mutex_unlock (pthread_mutex_t *mutex) ; 2.conditional Variable: Condition A. The condition must be protected by a mutex B. Type: pthread_cond_t, must be initialized to Pthread_ Cond_initializer (for statically assigned conditions, equivalent to PThread_cond_init (..., NULL)) or call pthread_cond_init#i nclude <pthread.h>1) int Pthread_cond_init ( pthread_cond_t *restrict cond, const PTHREAD_CONDXATTR _t *restrict attr) 2) int Pthread_cond_destroy (pthread_cond_t *cond); 3) int pthread_cond_wait ( pthread_cond_t *restrict cond, pthread_mutex_t * Restrict mutex); 4) int pthread_cond_timedwait ( pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct TIMESPEC *restrict timeout); 5) int pthread_cond_signal (pthread_cond_t *cond); 6) int Pthread_ Cond_broadcast (pthread_cond_t *cond);
Linux thread lock