| Skip Navigation Links | |
| Exit Print View | |
![]() | man pages section 3: Basic Library Functions Oracle Solaris 11 Information Library |
enable_extended_FILE_stdio(3C)
posix_spawnattr_getschedparam(3C)
posix_spawnattr_getschedpolicy(3C)
posix_spawnattr_getsigdefault(3C)
posix_spawnattr_getsigignore_np(3C)
posix_spawnattr_getsigmask(3C)
posix_spawnattr_setschedparam(3C)
posix_spawnattr_setschedpolicy(3C)
posix_spawnattr_setsigdefault(3C)
posix_spawnattr_setsigignore_np(3C)
posix_spawnattr_setsigmask(3C)
posix_spawn_file_actions_addclose(3C)
posix_spawn_file_actions_addclosefrom_np(3C)
posix_spawn_file_actions_adddup2(3C)
posix_spawn_file_actions_addopen(3C)
posix_spawn_file_actions_destroy(3C)
posix_spawn_file_actions_init(3C)
pthread_attr_getdetachstate(3C)
pthread_attr_getinheritsched(3C)
pthread_attr_getschedparam(3C)
pthread_attr_getschedpolicy(3C)
pthread_attr_setdetachstate(3C)
pthread_attr_setinheritsched(3C)
pthread_attr_setschedparam(3C)
pthread_attr_setschedpolicy(3C)
pthread_barrierattr_destroy(3C)
pthread_barrierattr_getpshared(3C)
pthread_barrierattr_setpshared(3C)
pthread_condattr_getpshared(3C)
pthread_condattr_setpshared(3C)
pthread_cond_reltimedwait_np(3C)
pthread_key_create_once_np(3C)
pthread_mutexattr_getprioceiling(3C)
pthread_mutexattr_getprotocol(3C)
pthread_mutexattr_getpshared(3C)
pthread_mutexattr_getrobust(3C)
pthread_mutexattr_setprioceiling(3C)
pthread_mutexattr_setprotocol(3C)
pthread_mutexattr_setpshared(3C)
pthread_mutexattr_setrobust(3C)
pthread_mutex_getprioceiling(3C)
pthread_mutex_reltimedlock_np(3C)
pthread_mutex_setprioceiling(3C)
pthread_rwlockattr_destroy(3C)
pthread_rwlockattr_getpshared(3C)
pthread_rwlockattr_setpshared(3C)
pthread_rwlock_reltimedrdlock_np(3C)
pthread_rwlock_reltimedwrlock_np(3C)
pthread_rwlock_timedrdlock(3C)
pthread_rwlock_timedwrlock(3C)
rctlblk_get_enforced_value(3C)
- lock or unlock a mutex
cc –mt [flag... ]file... –lpthread [library... ]#include <pthread.h>intpthread_mutex_lock(pthread_mutex_t *mutex);
intpthread_mutex_trylock(pthread_mutex_t *mutex);
intpthread_mutex_unlock(pthread_mutex_t *mutex);
The mutex object referenced by mutex is locked by callingpthread_mutex_lock(). Ifthe mutex is already locked, the calling thread blocks until the mutexbecomes available. This operation returns with the mutex object referenced by mutexin the locked state with the calling thread as its owner.
If the mutex type isPTHREAD_MUTEX_NORMAL, deadlock detection is not provided.Attempting to relock the mutex causes deadlock. If a thread attempts tounlock a mutex that it has not locked or a mutex thatis unlocked, undefined behavior results.
If the mutex type isPTHREAD_MUTEX_ERRORCHECK, then error checking is provided.If a thread attempts to relock a mutex that it has alreadylocked, an error will be returned. If a thread attempts to unlocka mutex that it has not locked or a mutex which isunlocked, an error will be returned.
If the mutex type isPTHREAD_MUTEX_RECURSIVE, then the mutex maintainsthe concept of a lock count. When a thread successfully acquires amutex for the first time, the lock count is set to 1.Every time a thread relocks this mutex, the lock count is incremented by one. Each time the thread unlocks the mutex, the lock countis decremented by one. When the lock count reaches0, themutex becomes available for other threads to acquire. If a thread attempts to unlock a mutex that it has not locked or amutex that is unlocked, an error will be returned.
If the mutex type isPTHREAD_MUTEX_DEFAULT, attempting to recursively lock themutex results in undefined behavior. Attempting to unlock the mutex ifit was not locked by the calling thread results in undefined behavior.Attempting to unlock the mutex if it is not locked results in undefinedbehavior.
Thepthread_mutex_trylock() function is identical topthread_mutex_lock() except that if the mutexobject referenced bymutex is currently locked (by any thread, including thecurrent thread), the call fails immediately withEBUSY.
Thepthread_mutex_unlock() function releases the mutex object referenced bymutex. The mannerin which a mutex is released is dependent upon the mutex's typeattribute. If there are threads blocked on the mutex object referenced bymutex whenpthread_mutex_unlock() is called, resulting in the mutex becoming available,the scheduling policy is used to determine which thread will acquirethe mutex. (In the case ofPTHREAD_MUTEX_RECURSIVE mutexes, the mutex becomesavailable when the count reaches0 and the calling thread no longerhas any locks on this mutex.)
If a signal is delivered to a thread waiting for a mutex,upon return from the signal handler the thread resumes waiting for themutex as if it was not interrupted.
If successful, thepthread_mutex_lock() andpthread_mutex_unlock() functions return0.Otherwise, an error number is returned to indicate the error.
Thepthread_mutex_trylock() function returns0 if a lock on themutex object referenced bymutex is acquired. Otherwise, an error number isreturned to indicate the error.
Thepthread_mutex_lock() andpthread_mutex_trylock() functions will fail if:
The mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded.
Themutex was created with the protocol attribute having the valuePTHREAD_PRIO_PROTECT and the calling thread's priority is higher than the mutex's current priority ceiling.
The mutex was created with the protocol attribute having the valuePTHREAD_PRIO_PROTECT and the calling thread is not in the real-time class (SCHED_RR orSCHED_FIFO scheduling class).
Thepthread_mutex_trylock() function will fail if:
Themutex could not be acquired because it was already locked.
Thepthread_mutex_lock(),pthread_mutex_trylock() andpthread_mutex_unlock() functions may fail if:
The value specified bymutex does not refer to an initialized mutex object.
Thepthread_mutex_lock() function may fail if:
The current thread already owns the mutex.
The limit on the number of simultaneously held mutexes has been exceeded.
Thepthread_mutex_unlock() function will fail if:
The mutex type is PTHREAD_MUTEX_ERRORCHECK or the mutex is a robust mutex, and the current thread does not own the mutex.
When a thread makes a call topthread_mutex_lock() orpthread_mutex_trylock(), if themutex is initialized with the robustness attribute having the valuePTHREAD_MUTEX_ROBUST (seepthread_mutexattr_getrobust(3C)), the call will return these error values if:
The last owner of this mutex died while holding the mutex, or the process containing the owner of the mutex unmapped the memory containing the mutex or performed one of theexec(2) functions. This mutex is now owned by the caller. The caller must now attempt to make the state protected by the mutex consistent. If it is able to clean up the state, then it should callpthread_mutex_consistent() for the mutex and unlock the mutex. Subsequent calls topthread_mutex_lock() andpthread_mutex_trylock() will behave normally, as before. If the caller is not able to clean up the state,pthread_mutex_consistent() should not be called for the mutex, but the mutex should be unlocked. Subsequent calls topthread_mutex_lock() andpthread_mutex_trylock() will fail to acquire the mutex with the error valueENOTRECOVERABLE. If the owner who acquired the lock withEOWNERDEAD dies, the next owner will acquire the lock withEOWNERDEAD.
The mutex trying to be acquired was protecting the state that has been left irrecoverable by the mutex's last owner. The mutex has not been acquired. This condition can occur when the lock was previously acquired withEOWNERDEAD, and the owner was not able to clean up the state and unlocked the mutex without callingpthread_mutex_consistent().
Seeattributes(5) for descriptions of the following attributes:
|
pthread_mutex_consistent(3C),pthread_mutex_init(3C),pthread_mutexattr_setprotocol(3C),pthread_mutexattr_setrobust(3C),pthread_mutexattr_settype(3C),attributes(5),standards(5)
In the current implementation of threads,pthread_mutex_lock(),pthread_mutex_unlock(),mutex_lock(),mutex_unlock(),pthread_mutex_trylock(), andmutex_trylock() do not validate the mutex type. Therefore, an uninitialized mutex ora mutex with an invalid type does not returnEINVAL. Interfacesfor mutexes with an invalid type have unspecified behavior.
Uninitialized mutexes that are allocated locally may contain junk data. Such mutexesneed to be initialized usingpthread_mutex_init() ormutex_init().
Copyright © 2011, Oracle and/or its affiliates. All rights reserved.Legal Notices | ![]() ![]() |