Three thread synchronization mechanisms • Mutex • semaphore • condition variable
pthread_t thread_id;
Main functions Pthread_create (), Pthread_exit (), Pthread_join (), Pthread_cancel ()
pthread_mutex_t Mutex;
The mutex consists mainly of the following basic functions: Mutex initialization: Pthread_mutex_init () Mutex Lock: Pthread_mutex_lock () Mutex Lock: Pthread_mutex_trylock () Mutex unlock: Pthread_mutex_unlock () Eliminate mutex: Pthread_mutex_destroy () The PV atomic operation used in the operating system is widely used for synchronization and mutual exclusion between processes or threads • Essentially a nonnegative integer counter, Used to control access to public resources PV atomic operations: Operation of the integer counter semaphore sem • One-time p operation causes the SEM to subtract one, while a V operation causes the SEM to add a process (or thread) to determine whether access to the public resource is available based on the value of the semaphore-when the value of the SEM is greater than or equal to zero, the A process (or thread) has access to a public resource – when the value of the semaphore SEM is less than zero, it blocks until the value of the semaphore SEM is greater than or equal to 0. PV operations are primarily used for synchronization and mutual exclusion between threads, and several threads only set a semaphore SEM synchronization, which sets multiple semaphores, Scheduling different initial values to implement the semaphore function between them sem_init () creates a semaphore and initializes it with sem_wait () and Sem_trywait (): p operation, minus the value of the semaphore when the semaphore is greater than zero • Difference: If the semaphore is less than zero, sem_wa It () will block the thread, sem_trywait () will immediately return Sem_post (): v operation, the value of the semaphore plus a simultaneous signal to wake up the waiting Thread Sem_getvalue (): Get the value of the Semaphore Sem_destroy (): Delete the signal volume
Linux Thread Memo