C++ Library, mos@ua  0.9
cpplib-mos
thread

POSIX threads library wrapper module. More...

Files

file  thread.h
 POSIX threads library wrapper module.
 

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.

This module removes defensive programming approach of native POSIX threads library.

All implemented functions, have exactly the same arguments and/or result of the original function, with the exception of returning an error indication.

Errors are handled by the implementation of two policies:

  1. EXIT_POLICY (default): describes the failed call in stderr (with the identification of the errno error, and the precise location the call), generates a segmentation fault (enabling a stack trace within a debugger like gdb), and exits program execution;
  2. EXCEPTION_POLICY: throws a int exception with the (errno) status error returned by the original function.
Author
Miguel Oliveira e Silva, 2017-2018

Function Documentation

◆ thread_equal()

int thread_equal ( pthread_t  t1,
pthread_t  t2 
)

pthread_equal wrapper function.

Documentation in

man 3 pthread_equal 
See also
https://man.cx/pthread_equal(3)

◆ thread_create()

void thread_create ( pthread_t *  t,
pthread_attr_t *  attr,
void *(*)(void *)  thread_main,
void *  arg 
)

pthread_create wrapper function.

Precondition:
t != NULL
thread_main != NULL

Documentation in

man 3 pthread_create 
See also
https://man.cx/pthread_create(3)

◆ thread_self()

pthread_t thread_self ( )

pthread_self wrapper function.

Documentation in

man 3 pthread_self 
See also
https://man.cx/pthread_self(3)

◆ thread_sched_yield()

void thread_sched_yield ( void  )

sched_yield wrapper function.

Documentation in

man 3 sched_yield 
See also
https://man.cx/sched_yield(3)

◆ thread_exit()

void thread_exit ( void *  retval)

pthread_exit wrapper function.

Documentation in

man 3 pthread_exit 
See also
https://man.cx/pthread_exit(3)

◆ thread_detach()

void thread_detach ( pthread_t  thread)

pthread_detach wrapper function.

Documentation in

man 3 pthread_detach 
See also
https://man.cx/pthread_detach(3)

◆ thread_join()

void thread_join ( pthread_t  t,
void **  result 
)

pthread_join wrapper function.

Documentation in

man 3 pthread_join 
See also
https://man.cx/pthread_join(3)

◆ mutex_init()

void mutex_init ( pthread_mutex_t *  pmtx,
pthread_mutexattr_t *  attr 
)

pthread_mutex_init wrapper function.

Precondition:
pmtx != NULL

Documentation in

man 3 pthread_mutex_init 
See also
https://man.cx/pthread_mutex_init(3)

◆ mutex_destroy()

void mutex_destroy ( pthread_mutex_t *  pmtx)

pthread_mutex_destroy wrapper function.

Precondition:
pmtx != NULL

Documentation in

man 3 pthread_mutex_destroy 
See also
https://man.cx/pthread_mutex_destroy(3)

◆ mutex_lock()

void mutex_lock ( pthread_mutex_t *  pmtx)

pthread_mutex_lock wrapper function.

Precondition:
pmtx != NULL

Documentation in

man 3 pthread_mutex_lock 
See also
https://man.cx/pthread_mutex_lock(3)

◆ mutex_trylock()

int mutex_trylock ( pthread_mutex_t *  pmtx)

pthread_mutex_trylock wrapper function.

Precondition:
pmtx != NULL

Documentation in

man 3 pthread_mutex_trylock 
See also
https://man.cx/pthread_mutex_trylock(3)
Returns
true (!=0) if lock succeeds, false (0) otherwise

◆ mutex_unlock()

void mutex_unlock ( pthread_mutex_t *  pmtx)

pthread_mutex_unlock wrapper function.

Precondition:
pmtx != NULL

Documentation in

man 3 pthread_mutex_unlock 
See also
https://man.cx/pthread_mutex_unlock(3)

◆ cond_init()

void cond_init ( pthread_cond_t *  pcvar,
pthread_condattr_t *  attr 
)

pthread_cond_init wrapper function.

Precondition:
pcvar != NULL

Documentation in

man 3 pthread_cond_init 
See also
https://man.cx/pthread_cond_init(3)

◆ cond_destroy()

void cond_destroy ( pthread_cond_t *  pcvar)

pthread_cond_destroy wrapper function.

Precondition:
pcvar != NULL

Documentation in

man 3 pthread_cond_destroy 
See also
https://man.cx/pthread_cond_destroy(3)

◆ cond_wait()

void cond_wait ( pthread_cond_t *  pcvar,
pthread_mutex_t *  pmtx 
)

pthread_cond_wait wrapper function.

Precondition:
pcvar != NULL
pmtx != NULL

Documentation in

man 3 pthread_cond_wait 
See also
https://man.cx/pthread_cond_wait(3)

◆ cond_timedwait()

int cond_timedwait ( pthread_cond_t *  pcvar,
pthread_mutex_t *  pmtx,
const struct timespec *  abstime 
)

pthread_cond_timedwait wrapper function.

Precondition:
pcvar != NULL
pmtx != NULL
abstime != NULL
Returns
true (!=0) if condition variable was signaled, false (0) it time out has expired.

Documentation in

man 3 pthread_cond_timedwait 
See also
https://man.cx/pthread_cond_wait(3)

◆ cond_signal()

void cond_signal ( pthread_cond_t *  pcvar)

pthread_cond_signal wrapper function.

Precondition:
pcvar != NULL

Documentation in

man 3 pthread_cond_signal 
See also
https://man.cx/pthread_cond_signal(3)

◆ cond_broadcast()

void cond_broadcast ( pthread_cond_t *  pcvar)

pthread_cond_broadcast wrapper function.

Precondition:
pcvar != NULL

Documentation in

man 3 pthread_cond_broadcast 
See also
https://man.cx/pthread_cond_broadcast(3)

◆ thread_once()

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

pthread_once wrapper function.

Precondition:
once_control != NULL
init_routine != NULL

Documentation in

man 3 pthread_once 
See also
https://man.cx/pthread_once(3)

◆ thread_key_create()

void thread_key_create ( pthread_key_t *  key,
void(*)(void *)  destr_function 
)

pthread_key_create wrapper function.

This function should be executed once for each key (use thread_once()).

Precondition:
key != NULL

Documentation in

man 3 pthread_key_create 
See also
https://man.cx/pthread_key_create(3)

◆ thread_key_delete()

void thread_key_delete ( pthread_key_t  key)

pthread_key_delete wrapper function.

This function should be executed once for each key (use thread_once()).

Precondition:
key != NULL

Documentation in

man 3 pthread_key_delete 
See also
https://man.cx/pthread_key_delete(3)

◆ thread_setspecific()

void thread_setspecific ( pthread_key_t  key,
void *  pointer 
)

pthread_setspecific wrapper function.

Precondition:
pointer != NULL

Documentation in

man 3 pthread_setspecific 
See also
https://man.cx/pthread_setspecific(3)

◆ thread_getspecific()

void* thread_getspecific ( pthread_key_t  key)

pthread_getspecific wrapper function.

Precondition:
key != NULL

Documentation in

man 3 pthread_getspecific 
See also
https://man.cx/pthread_getspecific(3)

◆ mutexattr_init()

void mutexattr_init ( pthread_mutexattr_t *  attr)

pthread_mutexattr_init wrapper function.

Precondition:
attr != NULL

Documentation in

man 3 pthread_mutexattr_init 
See also
https://man.cx/pthread_mutexattr_init(3)

◆ mutexattr_destroy()

void mutexattr_destroy ( pthread_mutexattr_t *  attr)

pthread_mutexattr_destroy wrapper function.

Precondition:
attr != NULL

Documentation in

man 3 pthread_mutexattr_destroy 
See also
https://man.cx/pthread_mutexattr_destroy(3)

◆ mutexattr_settype()

void mutexattr_settype ( pthread_mutexattr_t *  attr,
int  type 
)

pthread_mutexattr_settype wrapper function.

Precondition:
attr != NULL

Documentation in

man 3 pthread_mutexattr_settype 
See also
https://man.cx/pthread_mutexattr_settype(3)

◆ mutexattr_gettype()

void mutexattr_gettype ( const pthread_mutexattr_t *  attr,
int *  kind 
)

pthread_mutexattr_gettype wrapper function.

Precondition:
attr != NULL
kind != NULL

Documentation in

man 3 pthread_mutexattr_gettype 
See also
https://man.cx/pthread_mutexattr_gettype(3)

◆ condattr_init()

void condattr_init ( pthread_condattr_t *  attr)

pthread_condattr_init wrapper function.

Precondition:
attr != NULL

Documentation in

man 3 pthread_condattr_init 
See also
https://man.cx/pthread_condattr_init(3)

◆ condattr_destroy()

void condattr_destroy ( pthread_condattr_t *  attr)

pthread_condattr_destroy wrapper function.

Precondition:
attr != NULL

Documentation in

man 3 pthread_condattr_destroy 
See also
https://man.cx/pthread_condattr_destroy(3)

◆ thread_attr_init()

void thread_attr_init ( pthread_attr_t *  attr)

pthread_attr_init wrapper function.

Precondition:
attr != NULL

Documentation in

man 3 pthread_attr_init 
See also
https://man.cx/pthread_attr_init(3)

◆ thread_attr_destroy()

void thread_attr_destroy ( pthread_attr_t *  attr)

pthread_attr_destroy wrapper function.

Precondition:
attr != NULL

Documentation in

man 3 pthread_attr_destroy 
See also
https://man.cx/pthread_attr_destroy(3)

◆ thread_attr_setdetachstate()

void thread_attr_setdetachstate ( pthread_attr_t *  attr,
int  detachstate 
)

pthread_attr_setdetachstate wrapper function.

Precondition:
attr != NULL
detachstate == PTHREAD_CREATE_DETACHED || detachstate == PTHREAD_CREATE_JOINABLE

Documentation in

man 3 pthread_attr_setdetachstate 
See also
https://man.cx/pthread_attr_setdetachstate(3)

◆ thread_attr_getdetachstate()

void thread_attr_getdetachstate ( const pthread_attr_t *  attr,
int *  pdetachstate 
)

pthread_attr_getdetachstate wrapper function.

Precondition:
attr != NULL
pdetachstate != NULL

Documentation in

man 3 pthread_attr_getdetachstate 
See also
https://man.cx/pthread_attr_getdetachstate(3)

◆ thread_cancel()

void thread_cancel ( pthread_t  thread)

pthread_cancel wrapper function.

Documentation in

man 3 pthread_cancel 
See also
https://man.cx/pthread_cancel(3)

◆ thread_setcancelstate()

void thread_setcancelstate ( int  state,
int *  oldstate 
)

pthread_setcancelstate wrapper function.

Precondition:
state == PTHREAD_CANCEL_ENABLE || state == PTHREAD_CANCEL_DISABLE
oldstate != NULL

Documentation in

man 3 pthread_setcancelstate 
See also
https://man.cx/pthread_setcancelstate(3)

◆ thread_setcanceltype()

void thread_setcanceltype ( int  type,
int *  oldtype 
)

pthread_setcanceltype wrapper function.

Precondition:
type == PTHREAD_CANCEL_DEFERRED || type == PTHREAD_CANCEL_ASYNCHRONOUS
oldtype != NULL

Documentation in

man 3 pthread_setcanceltype 
See also
https://man.cx/pthread_setcanceltype(3)

◆ thread_testcancel()

void thread_testcancel ( void  )

pthread_testcancel wrapper function.

Documentation in

man 3 pthread_testcancel 
See also
https://man.cx/pthread_testcancel(3)