UNIX Environment Advanced Programming: Mutex of thread synchronization

Source: Internet
Author: User
Tags mutex resource thread

Mutexes, also known as mutexes, are derived from the POSIX thread standard and can be used to synchronize individual threads in the same process. Of course, if a mutex is stored in a single memory area shared by several processes, it can also be synchronized between processes through mutexes.

mutexes, which are literally known to be mutually exclusive, are the most basic synchronization tool for protecting critical areas (shared resources) to ensure that only one thread can access shared resources at any point in time.

The mutex type is declared as a pthread_mutex_t data type and has a specific definition in <bits/pthreadtypes.h>.

1 Mutex initialization and destruction

#include <pthread.h>  
int pthread_mutex_init (pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);  
      
int Pthread_mutex_destroy (pthread_mutex_t *mutex);  
                                              Return value: Returns 0 if successful, otherwise returns an error number

The above two functions are initialized and destroyed because of the mutex number respectively.

If the mutex is statically allocated, it can be initialized by a constant, as follows:

pthread_mutex_t mlock = Pthread_mutex_initializer;

You can also initialize by calling the Pthread_mutex_init function. If the mutex is dynamically allocated (for example, by calling the malloc function), the Pthread_mutex_destroy need to be invoked before the memory is freed.

To initialize the mutex with the default property, simply set the attr to null.

When you do not need to use mutexes, you need to invoke the resource used by the Pthread_mutex_destroy () to destroy the mutex.

2 Use of mutexes

#include <pthread.h>  
int pthread_mutex_trylock (pthread_mutex_t *mutex);  
      
int Pthread_mutex_lock (pthread_mutex_t *mutex);  
      
int Pthread_mutex_unlock (pthread_mutex_t *mutex);  
                                        Return value: Returns 0 if successful, otherwise returns an error number

To lock the mutex, you need to invoke Pthread_mutex_lock, and if the mutex is locked, the calling thread will block until the mutex is unlocked. Unlocking the mutex requires calling Pthread_mutex_unlock.

If the thread does not want to be blocked, it can use Pthread_mutex_trylock to attempt to lock the mutex. If the mutex is in an unlocked state when the call is Pthread_mutex_tyrlock, Pthread_mutex_trylock locks the mutex, does not block and returns 0, or the pthread_muxte_trylock fails. You cannot lock the mutex and return to Ebusy.

The emphasis here is that the mutex is used for locking and cannot be used for waiting.

Simply put, the use of mutexes should be: The thread takes up the mutex, then accesses the shared resource, and finally releases the mutex. Instead of: The thread occupies the mutex and then determines whether the resource is available, and if not, releases the mutex and repeats the process. This behavior, called rotation or polling, is a waste of CPU time.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/unix/

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.