Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
8.  Programming With Solaris Threads Similar Solaris Threads Functions Set the Thread Priority thr_setprio(3THR)  Previous   Contents   Next 
   
 

Get the Thread Priority

thr_getprio(3THR)

Use thr_getprio(3THR) to get the current priority for the thread. Each thread inherits a priority from its creator. thr_getprio() stores the current priority, tid, in the location pointed to by newprio. (For POSIX threads, see "pthread_getschedparam(3THR)".)

#include <thread.h>

int thr_getprio(thread_t tid, int *newprio)

Similar Synchronization Functions--Mutual Exclusion Locks

Initialize a Mutex

mutex_init(3THR)

#include <synch.h> 
#include <thread.h>

int mutex_init(mutex_t *mp, int type, void *arg)); 

Use mutex_init(3THR) to initialize the mutex pointed to by mp. The type can be one of the following (note that arg is currently ignored). (For POSIX threads, see "Initialize a Mutex".)

  • USYNC_PROCESS The mutex can be used to synchronize threads in this and other processes.

  • USYNC_PROCESS_ROBUST The mutex can be used to robustly synchronize threads in this and other processes.

  • USYNC_THREAD The mutex can be used to synchronize threads in this process only.

When a process dies while holding a USYNC_PROCESS lock, subsequent requestors of that lock hang. This is a problem for systems which share locks with client processes because the client processes can be abnormally killed. To avoid the problem of hanging on a lock held by a dead process, use USYNC_PROCESS_ROBUST to lock the mutex. USYNC_PROCESS_ROBUST adds two capabilities:

  • In the case of process death, all owned locks held by that process are unlocked.

  • The next requestor for any of the locks owned by the dead process receives the lock, but with an error return indicating that the previous owner died while holding the lock..

Mutexes 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 mutex simultaneously. A mutex lock must not be reinitialized while other threads might be using it.

Mutexes With Intraprocess Scope

#include <thread.h>

mutex_t mp;
int ret;

/* to be used within this process only */
ret = mutex_init(&mp, USYNC_THREAD, 0); 

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)".)

 
 
 
  Previous   Contents   Next