pthread_once(3T)pthread_once(3T)NAMEpthread_once() - call an initialization routine only once
SYNOPSIS
Parameters
once_control Pointer to the once-control object associated with the
one-time initialization function
init_routine The one-time initialization routine. This routine is
called only once, regardless of the number of times it
and its associated once_control are passed to
DESCRIPTION
The function guarantees that is only called one time in an application.
This function will use the once_control object to determine if has pre‐
viously been called via
The first time is called with once_control and causes to be called with
no arguments. Subsequent calls to with the same once_control will not
cause to be called again. When returns, the caller is guaranteed that
has been called (either just now or via a previous call).
The macro is used to statically initialize a once control block. This
initialization must be done before calling
is not a cancellation point. However, the caller supplied may be a
cancellation point. If the thread executing is canceled, the once_con‐
trol argument will be set to a state which indicates that has not been
called yet (see pthread_cancel(3T)). The next time the function is
called with once_control, the function will be called.
The behavior of is undefined if once_control has automatic storage
duration or is not initialized by
RETURN VALUE
returns returns the following values:
Successful completion.
Failure.
An error number is returned to indicate the error. (The
variable is not set.)
ERRORS
The following error value is returned by if the corresponding condition
is detected.
Either once_control or init_routine is invalid.
EXAMPLES
Some modules are designed for dynamic initialization, i.e., global ini‐
tialization is performed when the first function of the module is
invoked. In a single-threaded program, this is generally implemented
as follows:
(Rest of the code after initialization.)
For a multithreaded process, a simple initialization flag is not suffi‐
cient; the flag must be protected against modification by multiple
threads. Consequently, this flag has to be protected by a mutex that
has to be initialized only once, and so on. A multithreaded program
should use initialization similar to:
(Rest of the code after initialization.)
AUTHOR
was derived from the IEEE POSIX P1003.1c standard.
SEE ALSOpthread_create(3T).
STANDARDS CONFORMANCE
Pthread Library pthread_once(3T)