Skip Navigation Links | |
Exit Print View | |
Multithreaded Programming Guide Oracle Solaris 11.1 Information Library |
1. Covering Multithreading Basics
4. Programming with Synchronization Objects
5. Programming With the Oracle Solaris Software
6. Programming With Oracle Solaris Threads
Comparing APIs for Oracle Solaris Threads and POSIX Threads
Unique Oracle Solaris Threads Functions
Similar Synchronization Functions: Read-Write Locks
Initializing Read-Write Locks With Intraprocess Scope
Initializing Read-Write Locks With Interprocess Scope
Trying to Acquire a Write Lock
Destroying the Read-Write Lock State
Similar Oracle Solaris Threads Functions
Getting the Minimal Stack Size
Acquiring the Thread Identifier
Access the Signal Mask of the Calling Thread
Creating a Thread-Specific Data Key
Setting the Thread-Specific Data Value
Getting the Thread-Specific Data Value
Similar Synchronization Functions: Mutual Exclusion Locks
Mutexes With Intraprocess Scope
Mutexes With Interprocess Scope
Similar Synchronization Functions: Condition Variables
Initialize a Condition Variable
Condition Variables With Intraprocess Scope
Condition Variables With Interprocess Scope
Destroying a Condition Variable
cond_reltimedwait Return Values
Similar Synchronization Functions: Semaphores
Semaphores With Intraprocess Scope
Semaphores With Interprocess Scope
sema_destroy(3C) Return Values
Synchronizing Across Process Boundaries
Example of Producer and Consumer Problem
Special Issues for fork() and Oracle Solaris Threads
Initializing a mutex
Destroying a mutex
Acquiring a mutex
Releasing a mutex
Trying to acquire a mutex
Use mutex_init(3C) to initialize the mutex pointed to by mp. For POSIX threads, see Initializing a Mutex.
#include <synch.h> #include <thread.h> int mutex_init(mutex_t *mp, int type, void *arg));
The type can be one of the following values.
USYNC_PROCESS. The mutex can be used to synchronize threads in this process and other processes. arg is ignored.
USYNC_PROCESS_ROBUST. The mutex can be used to robustly synchronize threads in this process and other processes. arg is ignored.
USYNC_THREAD. The mutex can be used to synchronize threads in this process only. arg is ignored.
When a process fails while holding a USYNC_PROCESS lock, subsequent requestors of that lock hang. This behavior is a problem for systems that share locks with client processes because the client processes can be abnormally killed. To avoid the problem of hanging on a lock held by a dead process, use USYNC_PROCESS_ROBUST to lock the mutex. USYNC_PROCESS_ROBUST adds two capabilities:
In the case of process death, all owned locks held by that process are unlocked.
The next requestor for any of the locks owned by the failed process receives the lock. But, the lock is held with an error return indicating that the previous owner failed while holding the lock.
Mutexes can also be initialized by allocation in zeroed memory, in which case a type of USYNC_THREAD is assumed.
Multiple threads must not initialize the same mutex simultaneously. A mutex lock must not be reinitialized while other threads might be using the mutex.
#include <thread.h> mutex_t mp; int ret; /* to be used within this process only */ ret = mutex_init(&mp, USYNC_THREAD, 0);
#include <thread.h> mutex_t mp; int ret; /* to be used among all processes */ ret = mutex_init(&mp, USYNC_PROCESS, 0);
#include <thread.h> mutex_t mp; int ret; /* to be used among all processes */ ret = mutex_init(&mp, USYNC_PROCESS_ROBUST, 0);
mutex_init() returns 0 if successful. When any of the following conditions is detected, mutex_init() fails and returns the corresponding value.
EFAULT
Description: mp points to an illegal address.
EINVAL
Description: The value specified by mp is invalid.
ENOMEM
Description: System has insufficient memory to initialize the mutex.
EAGAIN
Description: System has insufficient resources to initialize the mutex.
EBUSY
Description: System detected an attempt to reinitialize an active mutex.
Use mutex_destroy(3C) to destroy any state that is associated with the mutex pointed to by mp . The space for storing the mutex is not freed. For POSIX threads, see pthread_mutex_destroy Syntax.
#include <thread.h> int mutex_destroy (mutex_t *mp);
mutex_destroy() returns 0 if successful. When the following condition is detected, mutex_destroy() fails and returns the corresponding value.
EFAULT
Description: mp points to an illegal address.
Use mutex_lock(3C) to lock the mutex pointed to by mp. When the mutex is already locked, the calling thread blocks until the mutex becomes available. Blocked threads wait on a prioritized queue. For POSIX threads, see pthread_mutex_lock Syntax.
#include <thread.h> int mutex_lock(mutex_t *mp);
mutex_lock() returns 0 if successful. When any of the following conditions is detected, mutex_lock() fails and returns the corresponding value.
EFAULT
Description: mp points to an illegal address.
EDEADLK
Description: The mutex is already locked and is owned by the calling thread.
Use mutex_unlock(3C) to unlock the mutex pointed to by mp. The mutex must be locked. The calling thread must be the thread that last locked the mutex, the owner. For POSIX threads, see pthread_mutex_unlock Syntax.
#include <thread.h> int mutex_unlock(mutex_t *mp);
mutex_unlock() returns 0 if successful. When any of the following conditions is detected, mutex_unlock() fails and returns the corresponding value.
EFAULT
Description: mp points to an illegal address.
EPERM
Description: The calling thread does not own the mutex.
Use mutex_trylock(3C) to attempt to lock the mutex pointed to by mp. This function is a nonblocking version of mutex_lock(). For POSIX threads, see pthread_mutex_trylock Syntax.
#include <thread.h> int mutex_trylock(mutex_t *mp);
mutex_trylock() returns 0 if successful. When any of the following conditions is detected, mutex_trylock() fails and returns the corresponding value.
EFAULT
Description: mp points to an illegal address.
EBUSY
Description: The system detected an attempt to reinitialize an active mutex.