Synchronization and mutex in linux when it comes to linux concurrency, it must involve synchronization and mutex between threads. linux mainly provides us with several mechanisms to achieve synchronization and mutex between threads, this article describes mutex, condition variables, and semaphores. Mutex locks and condition variables are included in the pthread thread library... synchronization and mutex in linux when it comes to linux concurrency, it must involve synchronization and mutex between threads. linux mainly provides us with several mechanisms to achieve synchronization and mutex between threads, this article describes mutex, condition variables, and semaphores. The mutex and condition variables are included in the pthread thread library. Header file. When using semaphores, you must include Header file. 1. mutex lock type declaration: pthread_mutex_t mutex; initialization of mutex: the program needs to initialize pthread_mutex_t before using pthread_mutex_t. for the statically allocated pthread_mutex_t variable, just assign PTHREAD_MUTEX_INITIALIZER to the variable. The statement is as follows: static pthread_mutex_t mutex = PTHREAD_mutex_INITIALIZER. for mutex variables that are dynamically allocated or do not have default mutex attributes, call the pthread_mutex_init function to execute the initial chemical operation. The function declaration is as follows: int pthread_mutex_init (pthread_mutex_t * mutex, const pthread_mutexattr_t * attr); mutex is a pointer to a mutex, and attr is a pointer to an attribute struct, if it is NULL, the default attribute is used. Note that the pthread_mutex_init function can only be executed exactly once. the result of repeated execution is undefined. Operation on mutex: int pthread_mutex_lock (pthread_mutex_t * mutex); // execute the lock operation on the mutex int pthread_mutex_unlock (pthread_mutex_t * mutex ); // unlock the mutex to destroy the mutex: int pthread_mutex_destroy (pthread_mutex_t * mutex); // Usage: The following code uses mutex to protect a critical zone: pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // initialize pthread_mutex_lock (& mutex); // Obtain the lock/* critical section code */pthread_mutex_unlock (& mutex ); // release the lock. a common method is to write a group of functions that access the critical section and put all lock calls in these functions. Number, which is a method to ensure the random access to the object, so that the locking mechanism is transparent to the calling thread. 2. conditional variables sometimes, we may need to keep the thread waiting until a certain condition is met. before the conditional variable is not used, you may use the busy waiting, as shown in the following code: while (x! = Y); however, this kind of behavior that occupies the pitfalls is very undesirable. Therefore, the concept of conditional variables is introduced to indicate that a thread is suspended when a condition is not met. Type Declaration: pthread_cond_t cond; initialization of the condition variable: the program must initialize it before using the pthread_cond_t variable. For the statically assigned pthread_cond_t variable, you only need to assign PTHREAD_COND_INITIALIZER to the variable. The statement is as follows: pthread_cond_t cond = PTHREAD_COND_INITIALIZER; for variables with dynamic allocation or no default attribute, call the pthread_cond_init function to perform initialization. The function declaration is as follows: int pthread_cond_init (pthread_cond_t * cond, const pthread_cond_attr_t * attr); cond is a pointer to a condition variable, and attr is a pointer to an attribute struct, if it is NULL, the default attribute is used. Operation on condition variables: int pthread_cond_wait (pthread_cond _ * cond, pthread_mutex_t * mutex); // causes the thread to block a condition. Int pthread_cond_signal (pthread_cond_t * cond); // wake up a thread that is blocked on the cond. Destruction of condition variables: int pthread_cond_destroy (pthread_cond_t * cond); usage: condition variables are called together with assertions or condition tests, in this way, the condition variable is associated with an asserted. Generally, the thread tests a condition. if it fails, it calls the pthread_cond_wait function to block the thread. The sample code is as follows: pthread_mutex_lock (& mutex); while (
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.