JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
Multithreaded Programming Guide     Oracle Solaris 11.1 Information Library
search filter icon
search icon

Document Information

Preface

1.  Covering Multithreading Basics

2.  Basic Threads Programming

3.  Thread Attributes

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

Major API Differences

Function Comparison Table

Unique Oracle Solaris Threads Functions

Suspending Thread Execution

thr_suspend Syntax

thr_suspend Return Values

Continuing a Suspended Thread

thr_continue Syntax

thr_continue Return Values

Similar Synchronization Functions: Read-Write Locks

Initialize a Read-Write Lock

rwlock_init Syntax

Initializing Read-Write Locks With Intraprocess Scope

Initializing Read-Write Locks With Interprocess Scope

rwlock_init Return Values

Acquiring a Read Lock

rw_rdlock Syntax

rw_rdlock Return Values

Trying to Acquire a Read Lock

rw_tryrdlock Syntax

rw_tryrdlock Return Values

Acquiring a Write Lock

rw_wrlock Syntax

rw_wrlock Return Values

Trying to Acquire a Write Lock

rw_trywrlock Syntax

rw_trywrlock Return Values

Unlock a Read-Write Lock

rw_unlock Syntax

rw_unlock Return Values

Destroying the Read-Write Lock State

rwlock_destroy Syntax

rwlock_destroy Return Values

Similar Oracle Solaris Threads Functions

Creating a Thread

thr_create Syntax

thr_create Return Values

Getting the Minimal Stack Size

thr_min_stack Syntax

thr_min_stack Return Values

Acquiring the Thread Identifier

thr_self Syntax

thr_self Return Values

Yield Thread Execution

thr_yield Syntax

thr_yield Return Values

Send a Signal to a Thread

thr_kill Syntax

thr_kill Return Values

Access the Signal Mask of the Calling Thread

thr_sigsetmask Syntax

thr_sigsetmask Return Values

Terminate a Thread

thr_exit Syntax

thr_exit Return Values

Wait for Thread Termination

thr_join Syntax

thr_join, Join Specific

thr_join, Join Any

thr_join Return Values

Creating a Thread-Specific Data Key

thr_keycreate Syntax

thr_keycreate Return Values

Setting the Thread-Specific Data Value

thr_setspecific Syntax

thr_setspecific Return Values

Getting the Thread-Specific Data Value

thr_getspecific Syntax

thr_getspecific Return Values

Set the Thread Priority

thr_setprio Syntax

thr_setprio Return Values

Get the Thread Priority

thr_getprio Syntax

thr_getprio Return Values

Similar Synchronization Functions: Mutual Exclusion Locks

Initialize a Mutex

mutex_init(3C) Syntax

Mutexes With Intraprocess Scope

Mutexes With Interprocess Scope

Mutexes With Interprocess Scope-Robust

mutex_init Return Values

Destroy a Mutex

mutex_destroy Syntax

mutex_destroy Return Values

Acquiring a Mutex

mutex_lock Syntax

mutex_lock Return Values

Releasing a Mutex

mutex_unlock Syntax

mutex_unlock Return Values

Trying to Acquire a Mutex

mutex_trylock Syntax

mutex_trylock Return Values

Similar Synchronization Functions: Condition Variables

Initialize a Condition Variable

cond_init Syntax

Condition Variables With Intraprocess Scope

Condition Variables With Interprocess Scope

cond_init Return Values

Destroying a Condition Variable

cond_destroy Syntax

cond_destroy Return Values

Waiting for a Condition

cond_wait Syntax

cond_wait Return Values

Wait for an Absolute Time

cond_timedwait Syntax

cond_timedwait Return Values

Waiting for a Time Interval

cond_reltimedwait Syntax

cond_reltimedwait Return Values

Unblock One Thread

cond_signal Syntax

cond_signal Return Values

Unblock All Threads

cond_broadcast Syntax

cond_broadcast Return Values

Similar Synchronization Functions: Semaphores

Initialize a Semaphore

sema_init Syntax

Semaphores With Intraprocess Scope

Semaphores With Interprocess Scope

sema_init Return Values

Increment a Semaphore

sema_post Syntax

sema_post Return Values

Block on a Semaphore Count

sema_wait Syntax

sema_wait Return Values

Decrement a Semaphore Count

sema_trywait Syntax

sema_trywait Return Values

Destroy the Semaphore State

sema_destroy(3C) Syntax

sema_destroy(3C) Return Values

Synchronizing Across Process Boundaries

Example of Producer and Consumer Problem

Special Issues for fork() and Oracle Solaris Threads

7.  Safe and Unsafe Interfaces

8.  Compiling and Debugging

9.  Programming Guidelines

A.  Extended Example: A Thread Pool Implementation

Index

Similar Synchronization Functions: Condition Variables

Initialize a Condition Variable

Use cond_init(3C) to initialize the condition variable pointed to by cv.

cond_init Syntax

#include <thread.h>

int cond_init(cond_t *cv, int type, int arg);

The type can be one of the following values:

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 .

Condition Variables With Intraprocess Scope
#include <thread.h>

cond_t cv;
int ret;

/* to be used within this process only */
ret = cond_init(cv, USYNC_THREAD, 0); 
Condition Variables With Interprocess Scope
#include <thread.h>

cond_t cv;
int ret;

/* to be used among all processes */
ret = cond_init(&cv, USYNC_PROCESS, 0); 

cond_init Return Values

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.

Destroying a Condition Variable

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.

cond_destroy Syntax

#include <thread.h>

int cond_destroy(cond_t *cv);

cond_destroy Return Values

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.

Waiting for a Condition

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.

cond_wait Syntax

#include <thread.h>

int cond_wait(cond_t *cv, mutex_t *mp);

cond_wait Return Values

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.

Wait for an Absolute Time

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.

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 Return Values

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.

Waiting for a Time Interval

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.

cond_reltimedwait Syntax

#include <thread.h>

int cond_reltimedwait(cond_t *cv, mutex_t *mp,
    timestruct_t reltime);

cond_reltimedwait Return Values

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.

Unblock One Thread

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.

cond_signal Syntax

#include <thread.h>

int cond_signal(cond_t *cv);

cond_signal Return Values

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.

Unblock All Threads

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.

cond_broadcast Syntax

#include <thread.h>

int cond_broadcast(cond_t *cv);

cond_broadcast Return Values

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.