C++ Library, mos@ua  0.9
cpplib-mos
thread.h File Reference

POSIX threads library wrapper module. More...

#include <pthread.h>

Go to the source code of this file.

Functions

Thread handling

Variable type: pthread_t

int thread_equal (pthread_t t1, pthread_t t2)
 pthread_equal wrapper function. More...
 
void thread_create (pthread_t *t, pthread_attr_t *attr, void *(*thread_main)(void *), void *arg)
 pthread_create wrapper function. More...
 
pthread_t thread_self ()
 pthread_self wrapper function. More...
 
void thread_sched_yield (void)
 sched_yield wrapper function. More...
 
void thread_exit (void *retval)
 pthread_exit wrapper function. More...
 
void thread_detach (pthread_t thread)
 pthread_detach wrapper function. More...
 
void thread_join (pthread_t t, void **result)
 pthread_join wrapper function. More...
 
Mutexes

Variable type: pthread_mutex_t

Initialization of mutexes can be static or dynamic.

In static initialization the following initialization macros are suggested:

  • PTHREAD_MUTEX_INITIALIZER - standard initializer
  • PTHREAD_MUTEX_ERRORCHECK - initializer that ensures error checking
  • PTHREAD_MUTEX_RECURSIVE - for recursive mutexes (note that recursive mutexes should be avoid)

Dynamic initialization is done by mutex_init() and mutex_destroy().

void mutex_init (pthread_mutex_t *pmtx, pthread_mutexattr_t *attr)
 pthread_mutex_init wrapper function. More...
 
void mutex_destroy (pthread_mutex_t *pmtx)
 pthread_mutex_destroy wrapper function. More...
 
void mutex_lock (pthread_mutex_t *pmtx)
 pthread_mutex_lock wrapper function. More...
 
int mutex_trylock (pthread_mutex_t *pmtx)
 pthread_mutex_trylock wrapper function. More...
 
void mutex_unlock (pthread_mutex_t *pmtx)
 pthread_mutex_unlock wrapper function. More...
 
Condition variables

Variable type: pthread_cond_t

Initialization of condition variables can be static or dynamic.

In static initialization the following initialization macros should be used:

  • PTHREAD_COND_INITIALIZER - standard initializer

Dynamic initialization is done by cond_init() and cond_destroy().

void cond_init (pthread_cond_t *pcvar, pthread_condattr_t *attr)
 pthread_cond_init wrapper function. More...
 
void cond_destroy (pthread_cond_t *pcvar)
 pthread_cond_destroy wrapper function. More...
 
void cond_wait (pthread_cond_t *pcvar, pthread_mutex_t *pmtx)
 pthread_cond_wait wrapper function. More...
 
int cond_timedwait (pthread_cond_t *pcvar, pthread_mutex_t *pmtx, const struct timespec *abstime)
 pthread_cond_timedwait wrapper function. More...
 
void cond_signal (pthread_cond_t *pcvar)
 pthread_cond_signal wrapper function. More...
 
void cond_broadcast (pthread_cond_t *pcvar)
 pthread_cond_broadcast wrapper function. More...
 
One-time initialization

Variable type: pthread_once_t

POSIX thread library support a mechanism that ensures a one-time execution of a function. It does it through pthread_once_t variables, and by passing a callback function (in C/C++ is simply a function pointer).

Example
   void once_init_routine(void) { ... }
   ...
   pthread_once_t once_control = PTHREAD_ONCE_INIT;
   thread_once(&once_control, &once_init_routine); // one time execution over pthread_once_t
                                                   // variable is guaranteed, regardless of the
                                                   // number of threads that execute over that
                                                   // variable.

void thread_once (pthread_once_t *once_control, void(*init_routine)(void))
 pthread_once wrapper function. More...
 
Thread-specific data

Variable type: pthread_key_t

Thread-specific data allows the definition of variable whose scope is limited to each thread. In practice, we will have a common variable, with a common access, but with different values for each thread.

void thread_key_create (pthread_key_t *key, void(*destr_function)(void *))
 pthread_key_create wrapper function. More...
 
void thread_key_delete (pthread_key_t key)
 pthread_key_delete wrapper function. More...
 
void thread_setspecific (pthread_key_t key, void *pointer)
 pthread_setspecific wrapper function. More...
 
void * thread_getspecific (pthread_key_t key)
 pthread_getspecific wrapper function. More...
 
Mutex attributes

Variable type: pthread_mutexattr_t

void mutexattr_init (pthread_mutexattr_t *attr)
 pthread_mutexattr_init wrapper function. More...
 
void mutexattr_destroy (pthread_mutexattr_t *attr)
 pthread_mutexattr_destroy wrapper function. More...
 
void mutexattr_settype (pthread_mutexattr_t *attr, int type)
 pthread_mutexattr_settype wrapper function. More...
 
void mutexattr_gettype (const pthread_mutexattr_t *attr, int *kind)
 pthread_mutexattr_gettype wrapper function. More...
 
Condition variables attributes

Variable type: pthread_condattr_t

void condattr_init (pthread_condattr_t *attr)
 pthread_condattr_init wrapper function. More...
 
void condattr_destroy (pthread_condattr_t *attr)
 pthread_condattr_destroy wrapper function. More...
 
Thread attributes

Variable type: pthread_attr_t

void thread_attr_init (pthread_attr_t *attr)
 pthread_attr_init wrapper function. More...
 
void thread_attr_destroy (pthread_attr_t *attr)
 pthread_attr_destroy wrapper function. More...
 
void thread_attr_setdetachstate (pthread_attr_t *attr, int detachstate)
 pthread_attr_setdetachstate wrapper function. More...
 
void thread_attr_getdetachstate (const pthread_attr_t *attr, int *pdetachstate)
 pthread_attr_getdetachstate wrapper function. More...
 
Cancellation

void thread_cancel (pthread_t thread)
 pthread_cancel wrapper function. More...
 
void thread_setcancelstate (int state, int *oldstate)
 pthread_setcancelstate wrapper function. More...
 
void thread_setcanceltype (int type, int *oldtype)
 pthread_setcanceltype wrapper function. More...
 
void thread_testcancel (void)
 pthread_testcancel wrapper function. More...
 

Detailed Description

POSIX threads library wrapper module.

Remarks
Removes defensive programming from native libraries