CALL_ONCE(3C) Standard C Library Functions CALL_ONCE(3C)

call_onceensure function is only called once

#include <treads.h>

once_flag once = ONCE_FLAG_INIT;

void
call_once(once_flag *once, void (*func)(void));

The () function is used to ensure that an operation occurs only once, even across multiple threads. Each instance of a properly initialized once_flag can be passed to the call_once function; however, only a single caller will successfully execute the specified function, func. This ensures that the argument func is called only once. Note, the argument once is the only thing used as a point of synchronization. If multiple callers use the same pointer for once, but use different values for func, then only one of the functions will be successfully called.

The argument () should always be initialized to the symbol before calling call_once(). Failure to do so will result in undefined behavior.

Like pthread_once(3C), the () function is not itself a cancellation point; however, if the thread calling () encounters a cancellation point and is cancelled, then the value pointed to by once will be as though call_once() had not been called, as func() had not completed successfully.

The call_once() function does not return any values. Upon its completion, it is guaranteed that func will have been called at most once across the liftime of the once argument .

pthread_once(3C), threads.h(3HEAD), attributes(7), threads(7)

February 17, 2023 OmniOS