PTHREAD_ONCE(3P)

🌸 PROLOG 🌸

okay so bestie, real talk first πŸ’… this page lives inside the POSIX Programmer's Manual, and it's all about pthread_once(3P). but heads up: the Linux implementation of this interface may differ (so like, go read the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface may not even be implemented on Linux at all. πŸ™ˆ


✨ NAME ✨

pthread_once β€” dynamic package initialization πŸŽ€ (yes, it's giving "do this thing exactly one time and never again", and honestly? iconic.)


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <pthread.h>

int pthread_once(pthread_once_t *once_control,
    void (*init_routine)(void));
pthread_once_t once_control = PTHREAD_ONCE_INIT;

πŸ’– DESCRIPTION πŸ’–

so here's the tea β˜• the first call to pthread_once() by any thread in a process, with a given once_control, shall call the init_routine with no arguments. periodt. then every call after that with the same once_control shall not call the init_routine β€” like, it already happened, we're not doing it again. πŸ’€

on return from pthread_once(), init_routine shall have completed. and the once_control parameter is the thing that shall determine whether the associated initialization routine has been called. it's literally the whole memory of the operation, bestie.

kay so important: pthread_once() is not a cancellation point. 🫢 However, if init_routine is a cancellation point and it gets canceled, the effect on once_control shall be as if pthread_once() was never called. like it never even happened, no cap.

and if the call to init_routine gets terminated by a call to longjmp(), _longjmp(), or siglongjmp() β€” the behavior is undefined. 🚨 undefined behavior is NOT the vibe. do not do this.

the constant PTHREAD_ONCE_INIT is defined in the <pthread.h> header. πŸ“Œ

also the behavior of pthread_once() is undefined if once_control has automatic storage duration or is not initialized by PTHREAD_ONCE_INIT. so like, initialize it properly or it's giving chaos. 😭


πŸ”₯ RETURN VALUE πŸ”₯

upon successful completion, pthread_once() shall return zero β€” slay. ✨ otherwise, an error number shall be returned to indicate the error. (note: an error number, not a -1-and-set-errno situation. different energy.)


πŸ’œ ERRORS πŸ’œ

pthread_once() shall not return an error code of [EINTR]. so don't go looking for that one, bestie, it's not coming. πŸ™…β€β™€οΈ

the following sections are informative.


πŸŽ€ EXAMPLES πŸŽ€

None.

(yeah. nothing. the void. empty. it's giving minimalist. πŸ’€)


🧚 APPLICATION USAGE 🧚

okay so this is the part where it gets a little cursed 😭

if init_routine recursively calls pthread_once() with the same once_control, the recursive call will not call the specified init_routine β€” because like, it already thinks it's in progress. which means the specified init_routine will not complete. which means the recursive call to pthread_once() will not return. forever. stuck. deadlock core. πŸ’€

and also: using longjmp(), _longjmp(), or siglongjmp() inside an init_routine to jump to a point outside of init_routine prevents init_routine from returning. so like, don't do the jump-scare. 🚨


πŸ‘‘ RATIONALE πŸ‘‘

so like, why does this function exist? history lesson time πŸ“–

some C libraries are designed for dynamic initialization. meaning: the global initialization for the library happens when the first procedure in the library gets called. in a single-threaded program, this is normally implemented using a static variable whose value gets checked on entry to a routine, like this:

static int random_is_initialized = 0;
extern void initialize_random(void);

int random_function()
{
    if (random_is_initialized == 0) {
        initialize_random();
        random_is_initialized = 1;
    }
    ... /* Operations performed after initialization. */
}

cute, right? but to keep the same structure in a multi-threaded program, a new primitive is needed. otherwise, library initialization has to be accomplished by an explicit call to a library-exported initialization function prior to any use of the library. 😬

so for dynamic library initialization in a multi-threaded process: if an initialization flag is used, the flag needs to be protected against modification by multiple threads simultaneously calling into the library. this can be done by using a mutex (initialized by assigning PTHREAD_MUTEX_INITIALIZER). However β€” and this is the main character moment β€” the better solution is to use pthread_once(), which is designed for exactly this purpose, like so:

#include <pthread.h>
static pthread_once_t random_is_initialized = PTHREAD_ONCE_INIT;
extern void initialize_random(void);

int random_function()
{
    (void) pthread_once(&random_is_initialized, initialize_random);
    ... /* Operations performed after initialization. */
}

and one more thing 🫢 if an implementation detects that the value specified by the once_control argument to pthread_once() does not refer to a pthread_once_t object initialized by PTHREAD_ONCE_INIT, it is recommended that the function should fail and report an [EINVAL] error. (recommended, not guaranteed β€” read that carefully bestie.)


🌈 FUTURE DIRECTIONS 🌈

None.

(again. nothing. the future is a blank canvas. ✨)


πŸ¦„ SEE ALSO πŸ¦„

  • The Base Definitions volume of POSIX.1‐2017, <pthread.h>

Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's the whole page, bestie πŸ’– run it once, run it right, and never think about it again. that's the entire point. 🫢

okay bye, this was brought to you by IEEE/The Open Group, 2017 🌟 mwah πŸ’…

πŸ“¦ where this page comes from