Executes a routine exactly once in a process.
Threads Library (libpthreads.a)
#include <pthread.h>
int pthread_once (once_control, init_routine)
pthread_once_t *once_control;
void (*init_routine)(void);
,
pthread_once_t once_control = PTHREAD_ONCE_INIT;
The pthread_once subroutine executes the routine init_routine exactly once in a process. The first call to this subroutine by any thread in the process executes the given routine, without parameters. Any subsequent call will have no effect.
The init_routine routine is typically an initialization routine. Multiple initializations can be handled by multiple instances of pthread_once_t structures. This subroutine is useful when a unique initialization has to be done by one thread among many. It reduces synchronization requirements.
Item | Description |
---|---|
once_control | Points to a synchronization control structure. This structure has to be initialized by the static initializer macro PTHREAD_ONCE_INIT. |
init_routine | Points to the routine to be executed. |
Upon successful completion, pthread_once returns zero. Otherwise, an error number is returned to indicate the error.
No errors are defined. The pthread_once function will not return an error code of EINTR.