Pthread
Pthread
POSIX.1c/D10 Summary
Disclaimer
Copyright (C) 1995 by Sun Microsystems, Inc. All rights reserved. This le is a product of SunSoft, Inc. and is provided for unrestricted use provided that this legend is included on all media and as a part of the software program in whole or part. Users may copy, modify or distribute this le at will. THIS FILE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. This le is provided with no support and without any obligation on the part of SunSoft, Inc. to assist in its use, correction, modication or enhancement. SUNSOFT AND SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS FILE OR ANY PART THEREOF. IN NO EVENT WILL SUNSOFT OR SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY LOST REVENUE OR PROFITS OR OTHER SPECIAL, INDIRECT AND CONSEQUENTIAL DAMAGES, EVEN IF THEY HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SunSoft, Inc. 2550 Garcia Avenue Mountain View, California 94043
where object is a type (not required if object is a thread), operation is a type-specic operation and np (or NP) is used to identify non-portable, implementation specic functions. All POSIX.1c functions (except for pthread_exit, pthread_getspecific and pthread_self) return zero (0) for success or an errno value if the operation fails. There are eight(8) POSIX.1c types:
Table 0-1 POSIX.1c types Type pthread_attr_t pthread_mutexattr_t pthread_condattr_t pthread_mutex_t pthread_cond_t pthread_t pthread_once_t pthread_key_t Description Thread attribute Mutual Exclusion Lock attribute Condition variable attribute Mutual Exclusion Lock (mutex) Condition variable (cv) Thread ID Once-only execution Thread Specic Data (TSD) key
Introduction
All source that uses POSIX.1c threads must include the header le.
#include <pthread.h>
In addition, Solaris requires the pre-processor symbol _REENTRANT to be dened in the source code before any C source (including header les).
#define_REENTRANT
The POSIX.1c thread library should be the last library specied on the cc(1) command line.
voyager% cc -D_REENTRANT ... -lpthread
_POSIX_THREAD_PROCESS_SHARED
Name Space
Each POSIX.1c type is of the form:
Macro Dependency
If _POSIX_THREAD_PRIO_INHERIT is dened then _POSIX_THREAD_PRIORITY_SCHEDULING is dened.
POSIX.1c/D10 Summary
If _POSIX_THREAD_PRIO_PROTECT is dened then _POSIX_THREAD_PRIORITY_SCHEDULING is dened. If _POSIX_THREAD_PRIORITY_SCHEDULING is dened then _POSIX_THREADS is dened. If _POSIX_THREADS is dened then _POSIX_THREAD_SAFE_FUNCTIONS is dened.
Table 0-3 Thread Attributes Name and Type void *stackaddr int detachstate Feature Test Macro _POSIX_THREAD_ATTR_STACKADDR _POSIX_THREADS Value(s) void *stack PTHREAD_CREATE_DETACHED, PTHREAD_CREATE_JOINABLE
POSIX.1c API
In the following sections, function arguments that are of the form:
type name = NULL
int
Thread Management
int pthread_create( pthread_t *thread, const pthread_attr_t *attr = NULL, void *(*entry)(void *), void *arg );
Register functions to be called during fork execution. errors ENOMEM notes prepare functions are called in reverse order of registration. parent and child functions are called in order of registration.
Thread Attributes
All thread attributes are set in an attribute object by a function of the form:
int pthread_attr_setname( pthread_attr_t *attr, Type t );
Create a new thread of execution. errors EAGAIN, EINVAL note Maximum number of PTHREAD_THREADS_MAX threads per process.
int pthread_detach( pthread_t thread );
Set the detachstate of the specied thread to PTHREAD_CREATE_DETACHED. errors EINVAL, ESRCH
pthread_t pthread_self( void );
All thread attributes are retrieved from an attribute object by a function of the form:
int pthread_attr_getname( const pthread_attr_t *attr, Type *t );
Synchronize with the termination of a thread. errors EINVAL, ESRCH, EDEADLK note This function is a cancellation point.
#include <sched.h> int pthread_getschedparam( pthread_t thread, int *policy, struct sched_param *param );
Get the scheduling policy and parameters of the specied thread. control _POSIX_THREAD_PRIORITY_SCHEDULING errors ENOSYS, ESRCH
#include <sched.h> int pthread_setschedparam( pthread_t thread, int policy, const struct sched_param *param );
POSIX.1c/D10 Summary
Set the scheduling policy and parameters of the specied thread. control _POSIX_THREAD_PRIORITY_SCHEDULING errors ENOSYS, EINVAL, ENOTSUP, EPERM, ESRCH policy { SCHED_RR, SCHED_FIFO, SCHED_OTHER }
Set the prioceiling value and return the old prioceiling value in the specied mutex. control _POSIX_THREAD_PRIO_PROTECT errors ENOSYS, EINVAL, EPERM
int pthread_mutex_lock( pthread_mutex_t *mutex );
Mutex Attributes
int
All mutex attributes are set in a mutex attribute object by a function of the form:
int pthread_mutexattr_setname( pthread_attr_t *attr, Type t ); int
All mutex attributes are retrieved from a mutex attribute object by a function of the form:
int pthread_mutexattr_getname( const pthread_attr_t *attr, Type *t );
Once-only Execution
pthread_once_t once = PTHREAD_ONCE_INIT;
All condition variable attributes are retreived from a condition variable attribute object by a function of the form:
int pthread_condattr_getname( const pthread_condattr_t *attr, Type *t );
Mutex Usage
int pthread_mutex_init( pthread_mutex_t *mutex, const pthread_mutexattr_t *attr = NULL ); mutex = PTHREAD_MUTEX_INITIALIZER; int pshared pthread_mutex_t
Get the prioceiling value of the specied mutex. control _POSIX_THREAD_PRIO_PROTECT errors ENOSYS, EINVAL, EPERM
int pthread_mutex_setprioceiling( pthread_mutex_t *mutex, int prioceiling, int *old_ceiling ); int
POSIX.1c/D10 Summary
errors how
#include <signal.h>
Unblock at least one thread currently blocked in the specied condition variable. errors EINVAL
int pthread_cond_broadcast( pthread_cond_t *cond );
Synchronously accept a signal. errors EINVAL, EINTR note This function is a cancellation point.
Unblock all threads currently blocked on the specied condition variable. errors EINVAL
int pthread_cond_wait( pthread_cond_t *cond, pthread_mutex_t *mutex );
Cancellation
int pthread_setcancelstate( int state, int *oldstate );
Block on the specied condition variable. errors EINVAL note This function is a cancellation point.
int pthread_cond_timedwait( pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime ); int
Set the cancellation state for the calling thread. errors EINVAL state { PTHREAD_CANCEL_ENABLE, PTHREAD_CANCEL_DISABLE }
pthread_setcanceltype( int type, int *oldtype );
Block on the specied condition variable not longer than the specied absolute time. errors ETIMEDOUT, EINVAL note This function is a cancellation point.
Set the cancellation type for the calling thread. errors EINVAL type { PTHREAD_CANCEL_DEFERRED, PTHREAD_CANCEL_ASYNCHRONOUS }
int pthread_cancel( pthread_t thread );
Cancel the specied thread. errors ESRCH note threads that have been cancelled terminate with a status of PTHREAD_CANCELED.
void pthread_testcancel( void );
Create a thread-specic data key. errors EAGAIN, ENOMEM note system limit of PTHREAD_KEYS_MAX per process. system limit of PTHREAD_DESTRUCTOR_ITERATIONS calls to destructor per thread exit.
int pthread_key_delete( pthread_key_t key );
Introduce a cancellation point. errors none note This function is a cancellation point.
void pthread_cleanup_pop( int execute );
item from the cancellation stack and optionally execute it. none specied push and pop operations must appear at the same lexical level. { 1, 0 }
Return the value bound to the given key for the calling thread. errors none
int pthread_setspecific( pthread_key_t key, const void *value );
Set the value for the given key in the calling thread. errors ENOMEM, EINVAL
Signal Management
#include <signal.h> int pthread_sigmask( int how, const sigset_t *newmask = NULL, sigset_t *oldmask = NULL );
POSIX.1c/D10 Summary