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

Comparing APIs for Oracle Solaris Threads and POSIX Threads

The Oracle Solaris threads API and the pthreads API are two solutions to the same problem: build parallelism into application software. Although each API is complete, you can safely mix Oracle Solaris threads functions and pthread functions in the same program.

The two APIs do not match exactly, however. Oracle Solaris threads support functions that are not found in pthreads, and pthreads include functions that are not supported in the Oracle Solaris interface. For those functions that do match, the associated arguments might not, although the information content is effectively the same.

By combining the two APIs, you can use features not found in one API to enhance the other API. Similarly, you can run applications that use Oracle Solaris threads exclusively with applications that use pthreads exclusively on the same system.

Major API Differences

Oracle Solaris threads and pthreads are very similar in both API action and syntax. The major differences are listed in Table 6-1 .

Table 6-1 Unique Oracle Solaris Threads and pthreads Features

Oracle Solaris Threads
POSIX Threads
thr_ prefix for threads function names, sema_ prefix for semaphore function names
pthread_ prefix for pthreads function names, sem_ prefix for semaphore function names
Ability to create “daemon” threads
Cancellation semantics
Suspending and continuing a thread
Scheduling policies

Function Comparison Table

The following table compares Oracle Solaris threads functions with pthreads functions. Note that even when Oracle Solaris threads and pthreads functions appear to be essentially the same, the arguments to the functions can differ.

When a comparable interface is not available either in pthreads or Oracle Solaris threads, a hyphen `-' appears in the column. Entries in the pthreads column that are followed by (3RT) are functions in librt, the POSIX.1b Realtime Extensions library, which is not part of pthreads. Functions in this library provide most of the interfaces specified by the POSIX.1b Realtime Extension.

Table 6-2 Oracle Solaris Threads and POSIX pthreads Comparison

Oracle Solaris Threads
pthreads
thr_create()
pthread_create()
thr_exit()
pthread_exit()
thr_join()
pthread_join()
thr_yield()
sched_yield()(3RT)
thr_self()
pthread_self()
thr_kill()
pthread_kill()
thr_sigsetmask()
pthread_sigmask()
thr_setprio()
pthread_setschedparam()
thr_getprio()
pthread_getschedparam()
thr_setconcurrency()
pthread_setconcurrency()
thr_getconcurrency()
pthread_getconcurrency()
thr_suspend()
-
thr_continue()
-
thr_keycreate()
pthread_key_create()
-
pthread_key_delete()
thr_setspecific()
pthread_setspecific()
thr_getspecific()
pthread_getspecific()
-
pthread_once()
-
pthread_equal()
-
pthread_cancel()
-
pthread_testcancel()
-
pthread_cleanup_push()
-
pthread_cleanup_pop()
-
pthread_setcanceltype()
-
pthread_setcancelstate()
mutex_lock()
pthread_mutex_lock()
mutex_unlock()
pthread_mutex_unlock()
mutex_trylock()
pthread_mutex_trylock()
mutex_init()
pthread_mutex_init()
mutex_destroy()
pthread_mutex_destroy()
cond_wait()
pthread_cond_wait()
cond_timedwait()
pthread_cond_timedwait()
cond_reltimedwait()
pthread_cond_reltimedwait_np()
cond_signal()
pthread_cond_signal()
cond_broadcast()
pthread_cond_broadcast()
cond_init()
pthread_cond_init()
cond_destroy()
pthread_cond_destroy()
rwlock_init()
pthread_rwlock_init()
rwlock_destroy()
pthread_rwlock_destroy()
rw_rdlock()
pthread_rwlock_rdlock()
rw_wrlock()
pthread_rwlock_wrlock()
rw_unlock()
pthread_rwlock_unlock()
rw_tryrdlock()
pthread_rwlock_tryrdlock()
rw_trywrlock()
pthread_rwlock_trywrlock()
-
pthread_rwlockattr_init()
-
pthread_rwlockattr_destroy()
-
pthread_rwlockattr_getpshared()
-
pthread_rwlockattr_setpshared()
sema_init()
sem_init()(3RT)
sema_destroy()
sem_destroy()(3RT)
sema_wait()
sem_wait()(3RT)
sema_post()
sem_post()(3RT)
sema_trywait()
sem_trywait()(3RT)
fork1()
fork()
-
pthread_atfork()
forkall(), multiple thread copy
-
-
pthread_mutexattr_init()
-
pthread_mutexattr_destroy()
type argument in mutex_init()
pthread_mutexattr_setpshared()
-
pthread_mutexattr_getpshared()
-
pthread_mutex_attr_settype()
-
pthread_mutex_attr_gettype()
-
pthread_condattr_init()
-
pthread_condattr_destroy()
type argument in cond_init()
pthread_condattr_setpshared()
-
pthread_condattr_getpshared()
-
pthread_attr_init()
-
pthread_attr_destroy()
THR_BOUND flag in thr_create()
pthread_attr_setscope()
-
pthread_attr_getscope()
-
pthread_attr_setguardsize()
-
pthread_attr_getguardsize()
stack_size argument in thr_create()
pthread_attr_setstacksize()
-
pthread_attr_getstacksize()
stack_addr argument in thr_create()
pthread_attr_setstack()
-
pthread_attr_getstack()
THR_DETACH flag in thr_create()
pthread_attr_setdetachstate()
-
pthread_attr_getdetachstate()
-
pthread_attr_setschedparam()
-
pthread_attr_getschedparam()
-
pthread_attr_setinheritsched()
-
pthread_attr_getinheritsched()
-
pthread_attr_setsschedpolicy()
-
pthread_attr_getschedpolicy()

To use the Oracle Solaris threads functions described in this chapter for Solaris 9 and previous releases, you must link with the Oracle Solaris threads library -lthread .

Operation is virtually the same for both Oracle Solaris threads and for pthreads, even though the function names or arguments might differ. Only a brief example consisting of the correct include file and the function prototype is presented. Where return values are not given for the Oracle Solaris threads functions, see the appropriate pages in man pages section 3: Basic Library Functions for the function return values.

For more information on Oracle Solaris related functions, see the related pthreads documentation for the similarly named function.

Where Oracle Solaris threads functions offer capabilities that are not available in pthreads, a full description of the functions is provided.