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
Mutexes With Interprocess Scope-Robust
Similar Synchronization Functions: Condition Variables
Initialize a Condition Variable
Condition Variables With Intraprocess Scope
Condition Variables With Interprocess Scope
Destroying a Condition Variable
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 condition variable
Destroying a condition variable
Waiting for a condition
Waiting for an absolute time
Waiting for a time interval
Unblocking one thread
Unblocking all threads
Use cond_init(3C) to initialize the condition variable pointed to by cv.
#include <thread.h> int cond_init(cond_t *cv, int type, int arg);
The type can be one of the following values:
USYNC_PROCESS. The condition variable can be used to synchronize threads in this process and other processes. arg is ignored.
USYNC_THREAD The condition variable can be used to synchronize threads in this process only. arg is ignored.
Condition variables 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 condition variable simultaneously. A condition variable must not be reinitialized while other threads might be using the condition variable.
For POSIX threads, see pthread_condattr_init Syntax .
#include <thread.h> cond_t cv; int ret; /* to be used within this process only */ ret = cond_init(cv, USYNC_THREAD, 0);
#include <thread.h> cond_t cv; int ret; /* to be used among all processes */ ret = cond_init(&cv, USYNC_PROCESS, 0);
cond_init() returns 0 if successful. When any of the following conditions is detected, cond_init() fails and returns the corresponding value.
EFAULT
Description: cv points to an illegal address.
EINVAL
Description: type is not a recognized type.
Use cond_destroy(3C) to destroy state that is associated with the condition variable pointed to by cv . The space for storing the condition variable is not freed. For POSIX threads, see pthread_condattr_destroy Syntax.
#include <thread.h> int cond_destroy(cond_t *cv);
cond_destroy() returns 0 if successful. When any of the following conditions is detected, cond_destroy() fails and returns the corresponding value.
EFAULT
Description: cv points to an illegal address.
EBUSY
Description: The system detected an attempt to destroy an active condition variable.
Use cond_wait(3C) to atomically release the mutex pointed to by mp and cause the calling thread to block on the condition variable pointed to by cv. The blocked thread can be awakened by cond_signal(), cond_broadcast() , or when interrupted by delivery of a signal or a fork().
cond_wait() always returns with the mutex locked and owned by the calling thread, even when returning an error.
#include <thread.h> int cond_wait(cond_t *cv, mutex_t *mp);
cond_wait() returns 0 if successful. When any of the following conditions is detected, cond_wait() fails and returns the corresponding value.
EFAULT
Description: cv points to an illegal address.
EINTR
Description: The wait was interrupted by a signal.
cond_timedwait(3C) is very similar to cond_wait(), except that cond_timedwait() does not block past the time of day specified by abstime . For POSIX threads, see pthread_cond_timedwait Syntax.
#include <thread.h> int cond_timedwait(cond_t *cv, mutex_t *mp, timestruct_t abstime);
cond_timedwait() always returns with the mutex locked and owned by the calling thread, even when returning an error.
The cond_timedwait() function blocks until the condition is signaled or until the time of day specified by the last argument has passed. The timeout is specified as the time of day so the condition can be retested efficiently without recomputing the time-out value.
cond_timedwait() returns 0 if successful. When any of the following conditions is detected, cond_timedwait() fails and returns the corresponding value.
EFAULT
Description: cv points to an illegal address.
ETIME
Description: The time specified by abstime has expired.
EINVAL
Description: abstime is invalid.
cond_reltimedwait(3C) is very similar to cond_timedwait(), except for the value for the third argument. cond_reltimedwait() takes a relative time interval value in its third argument rather than an absolute time of day value. For POSIX threads, see the pthread_cond_reltimedwait_np(3C) man page.
cond_reltimedwait() always returns with the mutex locked and owned by the calling thread even when returning an error. The cond_reltimedwait() function blocks until the condition is signaled or until the time interval specified by the last argument has elapsed.
#include <thread.h> int cond_reltimedwait(cond_t *cv, mutex_t *mp, timestruct_t reltime);
cond_reltimedwait() returns 0 if successful. When any of the following conditions is detected, cond_reltimedwait() fails and returns the corresponding value.
EFAULT
Description: cv points to an illegal address.
ETIME
Description: The time specified by reltime has expired.
Use cond_signal(3C) to unblock one thread that is blocked on the condition variable pointed to by cv . If no threads are blocked on the condition variable, cond_signal() has no effect.
#include <thread.h> int cond_signal(cond_t *cv);
cond_signal() returns 0 if successful. When the following condition is detected, cond_signal() fails and returns the corresponding value.
EFAULT
Description: cv points to an illegal address.
Use cond_broadcast(3C) to unblock all threads that are blocked on the condition variable pointed to by cv. When no threads are blocked on the condition variable, then cond_broadcast() has no effect.
#include <thread.h> int cond_broadcast(cond_t *cv);
cond_broadcast() returns 0 if successful. When the following condition is detected, cond_broadcast() fails and returns the corresponding value.
EFAULT
Description: cv points to an illegal address.