pthread_once(3)

pthread_once3

Dynamic package initialization

Usage

#include <pthread.h>

pthread_once_t once_control = PTHREAD_ONCE_INIT;

int

pthread_once(pthread_once_t *once_control, void (*init_routine)(void));

Library

POSIX Threads Library (libpthread, -lpthread)

Overview

The first call to pthread_once() by any thread in a process, with a given once_control, will call the init_routine() with no arguments. Subsequent calls to pthread_once() with the same once_control will not call the init_routine(). On return from pthread_once(), it is guaranteed that init_routine() has completed. The once_control parameter is used to determine whether the associated initialization routine has been called.

The function pthread_once() is not a cancellation point. However, if init_routine() is a cancellation point and is cancelled, the effect on once_control is as if pthread_once() was never called.

The constant PTHREAD_ONCE_INIT is defined by header ⟨pthread.h⟩.

The behavior of pthread_once() is undefined if once_control has automatic storage duration or is not initialized by PTHREAD_ONCE_INIT.

Return values

If successful, the pthread_once() function will return zero. Otherwise an error number will be returned to indicate the error.

Errors

None.

Releases

80 releases since FreeBSD 2.2.7-RELEASE; the text changed 46 times.

  1. 2.2.7
  2. 2.2.8–3.2
  3. 3.3–3.4
  4. 3.5–3.5.1
  5. 4.0
  6. 4.1
  7. 4.1.1–4.2
  8. 4.3
  9. 4.4
  10. 4.5–4.11
  11. 5.0
  12. 5.1
  13. 5.2–5.2.1
  14. 5.3–5.5
  15. 6.0–6.3
  16. 6.4
  17. 7.0
  18. 7.1
  19. 7.2
  20. 7.3
  21. 7.4
  22. 8.0
  23. 8.1
  24. 8.2
  25. 8.3
  26. 8.4
  27. 9.0
  28. 9.1
  29. 9.2
  30. 9.3
  31. 10.0
  32. 10.1
  33. 10.2
  34. 10.3
  35. 10.4
  36. 11.0
  37. 11.1
  38. 11.2
  39. 11.3
  40. 11.4
  41. 12.0
  42. 12.1
  43. 12.2
  44. 12.3
  45. 12.4
  46. 13.0–13.2
  47. 13.3–16.0

Standards

The pthread_once() function conforms to ISO/IEC 9945-1:1996 (“POSIX.1”).

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.