Lock function pthread_mutex_lock

Source: Internet
Author: User
Lock mutex

Use pthread_mutex_lock (3C) to lockMutexThe mutex.

Pthread_mutex_lockSyntax

intpthread_mutex_lock(pthread_mutex_t *mutex); 

#include <pthread.h>pthread_mutex_t mutex;int ret;ret = pthread_ mutex_lock(&mp); /* acquire the mutex */

WhenPthread_mutex_lock ()The mutex lock is locked. The Calling thread is the owner of the mutex. If the mutex lock has been locked and owned by another thread, the calling thread will be blocked until the mutex lock becomes available. For Solaris threads, seeMutex_lockSyntax.

If the mutex lock type isPTHREAD_MUTEX_NORMALDoes not provide Deadlock Detection. Attempts to lock the mutex lock again will lead to a deadlock. If a thread attempts to unlock the mutex lock is not locked by the thread or is not locked, it will produce uncertain behavior.

If the mutex lock type isPTHREAD_MUTEX_ERRORCHECK. If the mutex lock that a thread tries to lock has been locked by the thread, an error is returned. If a thread attempts to unlock the mutex lock is not locked by the thread or is not locked, an error is returned.

If the mutex lock type isPTHREAD_MUTEX_RECURSIVE, The mutex lock retains the lock count concept. When the thread gets the mutex lock for the first time, the lock count is set to 1. Every time the thread re-locks the mutex lock, the lock count increases by 1. Each time the thread unlocks the mutex lock, the lock count is reduced by 1. When the lock count reaches 0, the mutex lock can be obtained by other threads. If a thread attempts to unlock the mutex lock is not locked by the thread or is not locked, an error is returned.

If the mutex lock type isPTHREAD_MUTEX_DEFAULT, Attempts to recursively lock the mutex will produce uncertain behavior. For mutex locks that are not locked by the call thread, if you try to unlock the lock, it will produce uncertain behavior. If you try to unlock a mutex lock that has not yet been locked, an uncertain action will occur.

Pthread_mutex_lockReturn Value

Pthread_mutex_lock ()After successful completion, zero is returned. Any other returned values indicate an error. If any of the following conditions occurs, the function fails and the corresponding value is returned.

EAGAIN

Description:

The maximum number of recursive locks allowed for a mutex lock is exceeded.

EDEADLK

Description:

The current thread already has a mutex lock.

If_ POSIX_THREAD_PRIO_INHERITSymbol, the Protocol attribute value will be usedPTHREAD_PRIO_INHERITInitializes the mutex lock. In addition, ifPthread_mutexattr_setrobust_np ()OfRobustnessThe parameter isPTHREAD_MUTEX_ROBUST_NP, The function will fail and return one of the following values:

EOWNERDEAD

Description:

The last owner of the mutex fails to hold the mutex. The mutex is now owned by the caller. The caller must try to ensure that the status protected by the mutex is consistent.

If the caller can make the status consistent, call the mutex lockPthread_mutex_consistent_np ()And unlock the mutex. LaterPthread_mutex_lock ()Will be normal.

If the caller cannot make the status consistent, do not call the mutex lock.Pthread_mutex_init ()To unlock the mutex. Call laterPthread_mutex_lock ()Will not be able to obtain the mutex lock, and will return error codeENOTRECOVERABLE.

If the owner fails to obtain the lock and returnsEOWNERDEADWhen the next owner obtains the lockEOWNERDEAD.

ENOTRECOVERABLE

Description:

The mutex lock you are trying to obtain is protecting a certain state, which is irrecoverable because the previous owner of the mutex fails to hold the lock. You have not obtained the mutex. This situation may occur if the following conditions are met:

  • Returned when this lock was previously obtainedEOWNERDEAD

  • This owner cannot clear this status

  • The owner has unlocked the mutex lock, but the status of the mutex lock is not consistent.

ENOMEM

Description:

The number of mutex locks that can be held at the same time has been exceeded.

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.