Multithreaded Programming Guide

Calling an Initialization Routine for a Thread

Use pthread_once(3C) in a threaded process to call an initialization routine the first time pthread_once is called. Subsequent calls to pthread_once() from any thread in the process have no effect.

pthread_once Syntax

int  pthread_once(pthread_once_t *once_control, void (*init_routine)(void));
#include <pthread.h>

pthread_once_t once_control = PTHREAD_ONCE_INIT;
int ret;

ret = pthread_once(&once_control, 
init_routine);

The once_control parameter determines whether the associated initialization routine has been called.

pthread_once Return Values

pthread_once() returns zero after completing successfully. Any other return value indicates that an error occurred. When the following condition occurs, pthread_once() fails and returns the corresponding value.


EINVAL

Description:

once_control or init_routine is NULL.