Talk C chestnuts together (115th back: C language instance-mutex 1 for thread synchronization)

Source: Internet
Author: User

Talk C chestnuts together (115th back: C language instance-mutex 1 for thread synchronization)

Hello, everyone. We talked about the semaphores of thread synchronization in the last time. This example shows the mutex of thread synchronization. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

Today, we will introduce the use of mutex for thread synchronization.The core operation of mutex is lock/unlock.You can use related functions to perform the lock/unlock operation. Next we will introduce functions related to mutex.

Pthread_mutex_init Function
int pthread_mutex_init (pthread_mutex_t *__mutex,const pthread_mutexattr_t *__mutexattr)

This function is used to initialize mutex. This function has two parameters. Next we will introduce them separately.

The first parameter is a pointer to the mutex. The function initializes the mutex referred to by the pointer. The second parameter is a pointer to the mutex attribute, the function can change the value of the mutex property to the value of the variable referred to by the pointer. If the function runs successfully, zero is returned. Otherwise, the corresponding error code is returned. Pthread_mutex_destroy Function
int pthread_mutex_destroy (pthread_mutex_t *__mutex)
This function is used to destroy resources related to mutex. This function has only one parameter, which is a pointer to the mutex. The function will destroy resources related to the mutex; if the function runs successfully, zero is returned. Otherwise, the corresponding error code is returned. Lock Operation Function
int pthread_mutex_lock (pthread_mutex_t *__mutex)
This function is used to lock the mutex. This function has only one parameter, which is a pointer to the mutex. The function locks the mutex referred to by this pointer; if the function runs successfully, zero is returned. Otherwise, the corresponding error code is returned.

Note the following when using this function:

If the mutex has been locked, other threads will block the thread when using this function to lock until the mutex is unlocked. If the mutex has been locked, if the same thread locks the mutex again, a deadlock will occur;
int pthread_mutex_trylock (pthread_mutex_t *__mutex)
This function is used to apply an attempt to lock the mutex. This function has only one parameter, which is a pointer to the mutex. The function locks the mutex referred to by this pointer; if the function runs successfully, zero is returned. Otherwise, the corresponding error code is returned.

This function can be regarded as a supplement to the pthread_mutex_lock function. It attempts to lock the mutex. The meaning of the Retry is that if the mutex has been locked, then, it will not repeat the lock operation on the mutex. Therefore, the current thread will not be blocked, and no deadlock will occur. Instead, it will return EBUSY.

Pthread_mutex_unlock Function
int pthread_mutex_unlock (pthread_mutex_t *__mutex)
This function is used to unlock mutex. This function has only one parameter, which is a pointer to the mutex. The function will unlock the mutex referred to by this pointer; if the function runs successfully, zero is returned. Otherwise, the corresponding error code is returned.

Let's talk about the mutex of thread synchronization. I want to know what examples will be provided later, and I will try again.

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.