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
cond_reltimedwait Return Values
Similar Synchronization Functions: Semaphores
Semaphores With Intraprocess Scope
Synchronizing Across Process Boundaries
Example of Producer and Consumer Problem
Special Issues for fork() and Oracle Solaris Threads
Semaphore operations are the same in both the Oracle Solaris Operating Environment and the POSIX environment. The function name changed from sema_ in the Oracle Solaris Operating Environment to sem_ in pthreads. This section discusses the following topics:
Initializing a semaphore
Incrementing a semaphore
Blocking on a semaphore count
Decrementing a semaphore count
Destroying the semaphore state
Use sema_init(3C) to initialize the semaphore variable pointed to by sp by count amount.
#include <thread.h> int sema_init(sema_t *sp, unsigned int count, int type, void *arg);
type can be one of the following values:
USYNC_PROCESS. The semaphore can be used to synchronize threads in this process and other processes. Only one process should initialize the semaphore. arg is ignored.
USYNC_THREAD. The semaphore can be used to synchronize threads in this process, only. arg is ignored.
Multiple threads must not initialize the same semaphore simultaneously. A semaphore must not be reinitialized while other threads might be using the semaphore.
#include <thread.h> sema_t sp; int ret; int count; count = 4; /* to be used within this process only */ ret = sema_init(&sp, count, USYNC_THREAD, 0);
#include <thread.h> sema_t sp; int ret; int count; count = 4; /* to be used among all the processes */ ret = sema_init (&sp, count, USYNC_PROCESS, 0);
sema_init() returns 0 if successful. When any of the following conditions is detected, sema_init() fails and returns the corresponding value.
EINVAL
Description: sp refers to an invalid semaphore.
EFAULT
Description: Either sp or arg point to an illegal address.
Use sema_post(3C) to atomically increment the semaphore pointed to by sp. When any threads are blocked on the semaphore, one thread is unblocked.
#include <thread.h> int sema_post(sema_t *sp);
sema_post() returns 0 if successful. When any of the following conditions is detected, sema_post() fails and returns the corresponding value.
EINVAL
Description: sp refers to an invalid semaphore.
EFAULT
Description: sp points to an illegal address.
EOVERFLOW
Description: The semaphore value pointed to by sp exceeds SEM_VALUE_MAX.
Use sema_wait(3C) to block the calling thread until the count in the semaphore pointed to by sp becomes greater than zero. When the count becomes greater than zero, atomically decrement the count.
#include <thread.h> int sema_wait(sema_t *sp);
sema_wait() returns 0 if successful. When any of the following conditions is detected, sema_wait() fails and returns the corresponding value.
EINVAL
Description: sp refers to an invalid semaphore.
EINTR
Description: The wait was interrupted by a signal.
Use sema_trywait(3C) to atomically decrement the count in the semaphore pointed to by sp when the count is greater than zero. This function is a nonblocking version of sema_wait().
#include <thread.h> int sema_trywait(sema_t *sp);
sema_trywait() returns 0 if successful. When any of the following conditions is detected, sema_trywait() fails and returns the corresponding value.
EINVAL
Description: sp refers to an invalid semaphore.
EBUSY
Description: The semaphore pointed to by sp has a zero count.
Use sema_destroy(3C) to destroy any state that is associated with the semaphore pointed to by sp. The space for storing the semaphore is not freed.
#include <thread.h> int sema_destroy(sema_t *sp);
sema_destroy() returns 0 if successful. When the following condition is detected, sema_destroy() fails and returns the corresponding value.
EINVAL
Description: sp refers to an invalid semaphore.