Thread synchronization object in Linux (1) -- mutex

Source: Internet
Author: User

A process is an object for Linux Resource Allocation. Linux allocates virtual memory (4 GB), file handle, and other resources to the process. It is a static concept. Line
Is the object of CPU scheduling and a dynamic concept. A process must contain at least one or more threads. These threads share the memory and file handle resources of the process space.
Resources. To prevent inconsistent resource access by multiple threads, a very important task of multi-threaded programming is to control thread synchronization. This article briefly introduces the synchronization objects in Linux and some notes for use.

1. mutex)

Mutex is essentially a lock that protects one or more resources (data such as memory or file handle ). If a thread needs to access this resource, it must obtain the mutex and lock it. In this case
If a thread wants to access the resource, it must obtain the mutex, but the lock has been locked, so these processes can only block until the thread that obtains the lock is unlocked. In this case, a thread in the blocked thread obtains the mutex.
And lock to access the resource. Other threads continue to be blocked and start again.

The Linux mutex handle is pthread_mutex_t. You can use pthread_mutex_initializer to initialize a mutex, or call the following function to perform dynamic initialization:

#include <pthread.h>int pthread_mutex_init(pthread_mutex_t *restrict mutex,       const pthread_mutexattr_t *restrict attr);

Call the following function to destroy a mutex:

#include <pthread.h>int pthread_mutex_destroy(pthread_mutex_t *mutex);

The functions for locking and unlocking mutex are as follows:

       #include <pthread.h>       int pthread_mutex_lock(pthread_mutex_t *mutex);       int pthread_mutex_trylock(pthread_mutex_t *mutex);       int pthread_mutex_unlock(pthread_mutex_t *mutex);

When the current thread calls the pthread_mutex_lock function, if the mutex is not locked, the current thread obtains the mutex and unlocks it. This function returns. If the current mutex has been locked, the function is blocked until the mutex is unlocked. The current thread obtains the mutex and locks the result.

Pthread_mutex_trylock if the mutex is unlocked, the current thread will obtain the mutex and lock it. When the mutex is locked, the function immediately returns the error ebusy without blocking the current thread.

The unlock function of mutex is pthread_mutex_unlock, which releases mutex resources.

Another problem is the mutex deadlock (dead ).
Lock. When there is a mutex, no mutex problem occurs. When there are multiple mutex, a deadlock may occur. For example, A and B are mutually exclusive. Assume that the first thread obtains the mutex.
A, and lock. Then he tries to obtain the mutex B, but the mutex B has been locked, the thread is blocked, waiting for the mutex B. At the same time, another thread first obtains mutex B and has locked it. At this time, try to obtain mutex
Volume A. If mutex A is locked, the thread will be blocked and wait for mutex. In this case, the two threads wait for each other to obtain the semaphores, both of which are blocked and have deadlocks. How can this problem be solved?
What about this deadlock problem? That is, the thread obtains mutex in the same order. The first thread first obtains mutex A and then mutex B. The second thread also obtains mutex in the same order. This will not happen
Deadlock status.

However, in some complicated programs, it is difficult to obtain mutex in the same order. How can we solve the deadlock problem? Pthread_mutex_trylock is used to obtain the mutex. If the mutex cannot be obtained, release the mutex that has been held. After a while, try again to avoid deadlocks.


Related Article

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: 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.