pthread_mutex_lock.txt

来自「Pthread lib库完整说明文档」· 文本 代码 · 共 130 行

TXT
130
字号
           --------------------------------------------------------------------------------AIX Version 4.3 Base Operating System and Extensions Technical Reference, Volume                                       1--------------------------------------------------------------------------------pthread_mutex_lock, pthread_mutex_trylock, or pthread_mutex_unlock SubroutinePurposeLocks and unlocks a mutex.LibraryThreads Library (libpthreads.a)Syntax#include <pthread.h>int pthread_mutex_lock (mutex)pthread_mutex_t *mutex;int pthread_mutex_trylock (mutex)pthread_mutex_t *mutex;int pthread_mutex_unlock (mutex)pthread_mutex_t *mutex;DescriptionThe mutex object referenced by mutex is locked by calling pthread_mutex_lock. Ifthe mutex is already locked, the calling thread blocks until the mutex becomesavailable. This operation returns with the mutex object referenced by mutex inthe locked state with the calling thread as its owner.If the mutex type is PTHREAD_MUTEX_NORMAL, deadlock detection is not provided.Attempting to relock the mutex causes deadlock. If a thread attempts to unlock amutex that it has not locked or a mutex which is unlocked, undefined behaviourresults.If the mutex type is PTHREAD_MUTEX_ERRORCHECK, then error checking is provided.If a thread attempts to relock a mutex that it has already locked, an error willbe returned. If a thread attempts to unlock a mutex that it has not locked or amutex which is unlocked, an error will be returned.If the mutex type is PTHREAD_MUTEX_RECURSIVE, then the mutex maintains theconcept of a lock count. When a thread successfully acquires a mutex for thefirst time, the lock count is set to one. Every time a thread relocks this mutex,the lock count is incremented by one. Each time the thread unlocks the mutex, thelock count is decremented by one. When the lock count reaches zero, the mutexbecomes available for other threads to acquire. If a thread attempts to unlock amutex that it has not locked or a mutex which is unlocked, an error will bereturned.If the mutex type is PTHREAD_MUTEX_DEFAULT, attempting to recursively lock themutex results in undefined behaviour. Attempting to unlock the mutex if it wasnot locked by the calling thread results in undefined behaviour. Attempting tounlock the mutex if it is not locked results in undefined behaviour.The function pthread_mutex_trylock is identical to pthread_mutex_lock except thatif the mutex object referenced by mutex is currently locked (by any thread,including the current thread), the call returns immediately.The pthread_mutex_unlock function releases the mutex object referenced by mutex.The manner in which a mutex is released is dependent upon the mutex's typeattribute. If there are threads blocked on the mutex object referenced by mutexwhen pthread_mutex_unlock is called, resulting in the mutex becoming available,the scheduling policy is used to determine which thread shall acquire the mutex.(In the case of PTHREAD_MUTEX_RECURSIVE mutexes, the mutex becomes available whenthe count reaches zero and the calling thread no longer has any locks on thismutex).If a signal is delivered to a thread waiting for a mutex, upon return from thesignal handler the thread resumes waiting for the mutex as if it was notinterrupted.Parametermutex Specifies the mutex to lock.Return ValuesIf successful, the pthread_mutex_lock and pthread_mutex_unlock functions returnzero. Otherwise, an error number is returned to indicate the error.The function pthread_mutex_trylock returns zero if a lock on the mutex objectreferenced by mutex is acquired. Otherwise, an error number is returned toindicate the error.Error CodesThe pthread_mutex_trylock function will fail if:EBUSY The mutex could not be acquired because it was already locked.The pthread_mutex_lock, pthread_mutex_trylock and pthread_mutex_unlock functionswill fail if:EINVAL The value specified by mutex does not refer to an initialised mutexobject.The pthread_mutex_lock function will fail if:EDEADLK The current thread already owns the mutex and the mutex type ispthread_mutex_errorcheck.The pthread_mutex_unlock function will fail if:EPERM The current thread does not own the mutex and the mutex type is notpthread_mutex_normal.These functions will not return an error code of EINTR.Implementation SpecificsThese subroutines are part of the Base Operating System (BOS) Runtime.Related InformationThe pthread_mutex_init and pthread_mutex_destroy subroutines, pthread.h file.Using Mutexes and Threads Library Quick Reference in AIX Version 4.3 GeneralProgramming Concepts: Writing and Debugging Programs.--------------------------------------------------------------------------------           

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?