# pthread.h ## **Overview** **Related Modules:** [PROCESS](PROCESS.md) **Description:** Provides process- and thread-related structures \(providing fields such as thread attributes\) and functions \(including the functions for creating and destroying threads, and setting the thread detach state and blocking conditions\). **Since:** 1.0 **Version:** 1.0 ## **Summary** ## Functions

Function Name

Description

pthread_create (pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)

int 

Creates a thread.

pthread_detach (pthread_t thread)

int 

Detaches a thread.

pthread_exit (void *retval)

_Noreturn void 

Terminates the calling thread.

pthread_join (pthread_t thread, void **retval)

int 

Waits for a thread to terminate.

pthread_self (void)

pthread_t 

Obtains the ID of the calling thread.

pthread_equal (pthread_t t1, pthread_t t2)

int 

Compares whether two thread IDs are equal.

pthread_setcancelstate (int state, int *oldstate)

int 

Sets the cancelability state for the calling thread.

pthread_setcanceltype (int type, int *oldtype)

int 

Sets the cancelability type for the calling thread.

pthread_testcancel (void)

void 

Requests delivery of any pending cancellation request.

pthread_cancel (pthread_t thread)

int 

Sends a cancellation request to a thread.

pthread_kill (pthread_t thread, int sig)

int 

Sends a signal to a thread.

pthread_getschedparam (pthread_t thread, int *policy, struct sched_param *param)

int 

Obtains the scheduling policy and parameters of a thread.

pthread_setschedparam (pthread_t thread, int policy, const struct sched_param *param)

int 

Sets a scheduling policy and parameters for a thread.

pthread_setschedprio (pthread_t thread, int prio)

int 

Sets a static scheduling priority for a thread.

pthread_once (pthread_once_t *once_control, void(*init_routine)(void))

int 

Enables the initialization function to be called only once.

pthread_mutex_init (pthread_mutex_t *__restrict m, const pthread_mutexattr_t *__restrict a)

int 

Initializes a mutex.

pthread_mutex_lock (pthread_mutex_t *m)

int 

Locks a mutex.

pthread_mutex_unlock (pthread_mutex_t *m)

int 

Unlocks a mutex.

pthread_mutex_trylock (pthread_mutex_t *m)

int 

Attempts to lock a mutex.

pthread_mutex_timedlock (pthread_mutex_t *__restrict m, const struct timespec *__restrict at)

int 

Blocks the calling thread to lock a mutex.

pthread_mutex_destroy (pthread_mutex_t *m)

int 

Destroys a mutex.

pthread_cond_init (pthread_cond_t *__restrict c, const pthread_condattr_t *__restrict a)

int 

Initializes a condition variable.

pthread_cond_destroy (pthread_cond_t *c)

int 

Destroys a condition variable.

pthread_cond_wait (pthread_cond_t *__restrict c, pthread_mutex_t *__restrict m)

int 

Blocks the calling thread to wait for the condition set by pthread_con_signal().

pthread_cond_timedwait (pthread_cond_t *__restrict c, pthread_mutex_t *__restrict m, const struct timespec *__restrict ts)

int 

Blocks the calling thread to wait for the condition set by pthread_con_signal() for a period of time specified by ts.

pthread_cond_broadcast (pthread_cond_t *c)

int 

Unblocks all threads that are currently blocked on the condition variable cond.

pthread_cond_signal (pthread_cond_t *c)

int 

Unblocks a thread.

pthread_rwlock_init (pthread_rwlock_t *__restrict rw, const pthread_rwlockattr_t *__restrict a)

int 

Initializes a read-write lock.

pthread_rwlock_destroy (pthread_rwlock_t *rw)

int 

Destroys a read-write lock.

pthread_rwlock_rdlock (pthread_rwlock_t *rw)

int 

Applies a read lock to a read-write lock.

pthread_rwlock_tryrdlock (pthread_rwlock_t *rw)

int 

Attempts to apply a read lock to a read-write lock.

pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict rw, const struct timespec *__restrict at)

int 

Blocks the calling thread to lock a read-write lock for reading.

pthread_rwlock_wrlock (pthread_rwlock_t *rw)

int 

Applies a write lock to a read-write lock.

pthread_rwlock_trywrlock (pthread_rwlock_t *rw)

int 

Attempts to apply a write lock to a read-write lock.

pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict rw, const struct timespec *__restrict at)

int 

Blocks the calling thread to lock a read-write lock for writing.

pthread_rwlock_unlock (pthread_rwlock_t *rw)

int 

Unlocks a read-write lock.

pthread_spin_init (pthread_spinlock_t *s, int shared)

int 

Initializes a spin lock.

pthread_spin_destroy (pthread_spinlock_t *s)

int 

Destroys a spin lock.

pthread_spin_lock (pthread_spinlock_t *s)

int 

Locks a spin lock.

pthread_spin_trylock (pthread_spinlock_t *s)

int 

Attempts to lock a spin lock.

pthread_spin_unlock (pthread_spinlock_t *s)

int 

Unlocks a spin lock.

pthread_barrier_init (pthread_barrier_t *__restrict b, const pthread_barrierattr_t *__restrict a, unsigned count)

int 

Initializes a barrier.

pthread_barrier_destroy (pthread_barrier_t *b)

int 

Destroys a barrier.

pthread_barrier_wait (pthread_barrier_t *b)

int 

Synchronizes participating threads at a barrier.

pthread_key_create (pthread_key_t *key, void(*destructor)(void *))

int 

Creates a key for thread data.

pthread_key_delete (pthread_key_t key)

int 

Deletes a key for thread data.

pthread_getspecific (pthread_key_t key)

void * 

Obtains specific thread data.

pthread_setspecific (pthread_key_t key, const void *value)

int 

Sets specific thread data.

pthread_attr_init (pthread_attr_t *attr)

int 

Initializes a thread attribute object.

pthread_attr_destroy (pthread_attr_t *attr)

int 

Destroys a thread attribute object.

pthread_attr_getguardsize (const pthread_attr_t *attr, size_t *guardsize)

int 

Obtains the guard size of a thread attribute object.

pthread_attr_setguardsize (pthread_attr_t *attr, size_t guardsize)

int 

Sets the guard size for a thread attribute object.

pthread_attr_getstacksize (const pthread_attr_t *attr, size_t *stacksize)

int 

Obtains the stack size of a thread attribute object.

pthread_attr_setstacksize (pthread_attr_t *attr, size_t stacksize)

int 

Sets the stack size for a thread attribute object.

pthread_attr_getdetachstate (const pthread_attr_t *attr, int *detachstate)

int 

Obtains the detach state of a thread attribute object.

pthread_attr_setdetachstate (pthread_attr_t *attr, int detachstate)

int 

Sets the detach state for a thread attribute object.

pthread_attr_getstack (const pthread_attr_t *attr, void **stackaddr, size_t *stacksize)

int 

Obtains stack attributes of a thread attribute object.

pthread_attr_setstack (pthread_attr_t *attr, void *stackaddr, size_t stacksize)

int 

Sets stack attributes for a thread attribute object.

pthread_attr_getscope (const pthread_attr_t *arrt, int *scope)

int 

Obtains contention scope attributes of a thread attribute object.

pthread_attr_setscope (pthread_attr_t *arrt, int scope)

int 

Sets contention scope attributes for a thread attribute object.

pthread_attr_getschedpolicy (const pthread_attr_t *attr, int *schedpolicy)

int 

Obtains scheduling policy attributes of a thread attribute object.

pthread_attr_setschedpolicy (pthread_attr_t *attr, int schedpolicy)

int 

Sets scheduling policy attributes for a thread attribute object.

pthread_attr_getschedparam (const pthread_attr_t *attr, struct sched_param *param)

int 

Obtains scheduling parameter attributes of a thread attribute object.

pthread_attr_setschedparam (pthread_attr_t *attr, const struct sched_param *param)

int 

Sets scheduling parameter attributes for a thread attribute object.

pthread_attr_getinheritsched (const pthread_attr_t *attr, int *inheritsched)

int 

Obtains inherit scheduler attributes of a thread attribute object.

pthread_attr_setinheritsched (pthread_attr_t *attr, int inheritsched)

int 

Sets inherit scheduler attributes for a thread attribute object.

pthread_mutexattr_destroy (pthread_mutexattr_t *attr)

int 

Destroys a mutex attribute object.

pthread_mutexattr_gettype (const pthread_mutexattr_t *__restrict attr, int *__restrict type)

int 

Obtains the mutex type attribute.

pthread_mutexattr_init (pthread_mutexattr_t *attr)

int 

Initializes a mutex attribute object.

pthread_mutexattr_settype (pthread_mutexattr_t *attr, int type)

int 

Sets the mutex type attribute.

pthread_condattr_init (pthread_condattr_t *a)

int 

Initializes a condition variable attribute object.

pthread_condattr_destroy (pthread_condattr_t *a)

int 

Destroys a condition variable attribute object.

pthread_condattr_setclock (pthread_condattr_t *a, clockid_t clk)

int 

Sets a clock for a condition variable attribute object.

pthread_condattr_getclock (const pthread_condattr_t *__restrict a, clockid_t *__restrict clk)

int 

Obtains the clock of a condition variable attribute object.

pthread_rwlockattr_init (pthread_rwlockattr_t *attr)

int 

Initializes a read-write lock attribute object.

pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr)

int 

Destroys a read-write lock attribute object.

pthread_barrierattr_destroy (pthread_barrierattr_t *a)

int 

Destroys a barrier attribute object.

pthread_barrierattr_init (pthread_barrierattr_t *a)

int 

Initializes a barrier attribute object.

pthread_atfork (void(*prepare)(void), void(*parent)(void), void(*child)(void))

int 

Registers a fork handler to be called before and after fork().

pthread_cleanup_push (void(*routine)(void *), void *arg)

void 

Pushes the routine to the top of the clean-up handler stack.

pthread_cleanup_pop (int execute)

void 

Removes the routine at the top of the clean-up handler stack.

pthread_getattr_np (pthread_t thread, pthread_attr_t *attr)

int 

Obtains the attributes of a created thread.

pthread_setname_np (pthread_t pthread, const char *name)

int 

Sets the thread name.