Sun Microsystems Logo
Products and Services
 
Support and Training
 
 

Previous Previous     Contents     Index     Next Next

Mutexes With Interprocess Scope

#include <thread.h>

mutex_t mp;
int ret;

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

Mutexes With Interprocess Scope-Robust

#include <thread.h>

mutex_t mp;
int ret;

/* to be used among all processes */
ret = mutex_init(&mp, USYNC_PROCESS_ROBUST, 0); 

Destroy a Mutex

mutex_destroy(3THR)

#include <thread.h>

int mutex_destroy (mutex_t *mp);

Use mutex_destroy(3THR) to destroy any state associated with the mutex pointed to by mp. Note that the space for storing the mutex is not freed. (For POSIX threads, see pthread_mutex_destroy(3THR).)

Acquire a Mutex

mutex_lock(3THR)

#include <thread.h>

int mutex_lock(mutex_t *mp);

Use mutex_lock(3THR) 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(3THR).)

Release a Mutex

mutex_unlock(3THR)

#include <thread.h>

int mutex_unlock(mutex_t *mp);

Use mutex_unlock(3THR) to unlock the mutex pointed to by mp. The mutex must be locked and the calling thread must be the one that last locked the mutex (the owner). (For POSIX threads, see pthread_mutex_unlock(3THR).)

Try to Acquire a Mutex

mutex_trylock(3THR)

#include <thread.h>

int mutex_trylock(mutex_t *mp);

Use mutex_trylock(3THR) 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(3THR).)

Similar Synchronization Functions--Condition Variables

Initialize a Condition Variable

cond_init(3THR)

#include <thread.h>

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

Use cond_init(3THR) to initialize the condition variable pointed to by cv. The type can be one of the following (note that arg is currently ignored). (For POSIX threads, see pthread_condattr_init(3THR).)

  • USYNC_PROCESS The condition variable can be used to synchronize threads in this 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 it.

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); 

Destroy a Condition Variable

cond_destroy(3THR)

#include <thread.h>

int cond_destroy(cond_t *cv);

Use cond_destroy(3THR) to destroy state 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(3THR).)

Wait for a Condition

cond_wait(3THR)

#include <thread.h>

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

Use cond_wait(3THR) to atomically release the mutex pointed to by mp and to 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(). (For POSIX threads, see pthread_cond_wait(3THR).)

Previous Previous     Contents     Index     Next Next