Linux C multi-thread programming mutex lock and condition variable

Source: Internet
Author: User

From: http://www.linuxidc.com/Linux/2011-08/39987.htm

I. mutex lock

In essence, mutex is a lock that provides protection for access to shared resources.

1. Initialization:

In Linux, the thread mutex data type is pthread_mutex_t. Before use, initialize it:

For the mutex of static allocation, you can set it to pthread_mutex_initializer or call pthread_mutex_init.

For the dynamically allocated mutex, after applying for memory (malloc), initialize with pthread_mutex_init and call pthread_mutex_destroy before releasing the memory (free.

Prototype:

Int pthread_mutex_init (pthread_mutex_t * restrict mutex, const pthread_mutexattr_t * restric ATTR );

Int pthread_mutex_destroy (pthread_mutex_t * mutex );

Header file:

Return Value: 0 is returned for success, and error number is returned for error.

Note: If you use the default attribute to initialize mutex, you only need to set ATTR to null. Other values will be explained later.

2. mutex operation:

To access shared resources, You need to lock the mutex. If the mutex has been locked, the calling thread will be blocked until the mutex is unlocked. after accessing shared resources, unlock the mutex.

First, let's talk about the locking function:

Header file:

Prototype:

Int pthread_mutex_lock (pthread_mutex_t * mutex );

Int pthread_mutex_trylock (pthread_mutex_t * mutex );

Return Value: 0 is returned for success, and error number is returned for error.

Description: The trylock function is a non-blocking call mode. That is, if the mutex is not locked, the trylock function locks the mutex, and get access to the shared resources. If the mutex is locked, the trylock function will not block the wait and return ebusy directly, indicating that the shared resources are busy.

Let's talk about the function:

Header file:

Prototype: int pthread_mutex_unlock (pthread_mutex_t * mutex );

Return Value: 0 is returned for success, and error number is returned for error.

3. deadlock:

A deadlock occurs when multiple dependency locks exist, and occurs when a thread tries to lock the mutex in the reverse order of the other thread. It is important to avoid a deadlock when mutex is used.

In general, there are several unwritten basic principles:

You must obtain the lock before performing operations on shared resources.

The lock must be released after the operation is completed.

Use the lock as soon as possible.

If there are multiple locks, for example, if the order of acquisition is ABC, the release order should also be ABC.

When a thread returns an error, it should release the lock it obtained.

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