1PTHREAD_ONCE(3P)           POSIX Programmer's Manual          PTHREAD_ONCE(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       pthread_once - dynamic package initialization
13

SYNOPSIS

15       #include <pthread.h>
16
17       int pthread_once(pthread_once_t *once_control,
18              void (*init_routine)(void));
19       pthread_once_t once_control = PTHREAD_ONCE_INIT;
20
21

DESCRIPTION

23       The first call to pthread_once() by any thread in  a  process,  with  a
24       given once_control, shall call the init_routine with no arguments. Sub‐
25       sequent calls of pthread_once() with the same  once_control  shall  not
26       call  the  init_routine.  On  return  from pthread_once(), init_routine
27       shall  have  completed.  The  once_control  parameter  shall  determine
28       whether the associated initialization routine has been called.
29
30       The  pthread_once()  function  is not a cancellation point. However, if
31       init_routine is a cancellation point and is  canceled,  the  effect  on
32       once_control shall be as if pthread_once() was never called.
33
34       The constant PTHREAD_ONCE_INIT is defined in the <pthread.h> header.
35
36       The  behavior  of pthread_once() is undefined if once_control has auto‐
37       matic storage duration or is not initialized by PTHREAD_ONCE_INIT.
38

RETURN VALUE

40       Upon successful completion, pthread_once() shall  return  zero;  other‐
41       wise, an error number shall be returned to indicate the error.
42

ERRORS

44       The pthread_once() function may fail if:
45
46       EINVAL If either once_control or init_routine is invalid.
47
48
49       The pthread_once() function shall not return an error code of [EINTR].
50
51       The following sections are informative.
52

EXAMPLES

54       None.
55

APPLICATION USAGE

57       None.
58

RATIONALE

60       Some  C libraries are designed for dynamic initialization. That is, the
61       global initialization for the library is performed when the first  pro‐
62       cedure  in the library is called. In a single-threaded program, this is
63       normally implemented using a static variable whose value is checked  on
64       entry to a routine, as follows:
65
66
67              static int random_is_initialized = 0;
68              extern int initialize_random();
69
70
71              int random_function()
72              {
73                  if (random_is_initialized == 0) {
74                      initialize_random();
75                      random_is_initialized = 1;
76                  }
77                  ... /* Operations performed after initialization. */
78              }
79
80       To keep the same structure in a multi-threaded program, a new primitive
81       is needed. Otherwise, library initialization has to be accomplished  by
82       an explicit call to a library-exported initialization function prior to
83       any use of the library.
84
85       For dynamic library initialization in a multi-threaded process, a  sim‐
86       ple  initialization  flag  is not sufficient; the flag needs to be pro‐
87       tected against modification by multiple threads simultaneously  calling
88       into the library. Protecting the flag requires the use of a mutex; how‐
89       ever, mutexes have to be initialized before  they  are  used.  Ensuring
90       that  the  mutex is only initialized once requires a recursive solution
91       to this problem.
92
93       The use of pthread_once() not only supplies  an  implementation-guaran‐
94       teed  means  of dynamic initialization, it provides an aid to the reli‐
95       able construction of multi-threaded and realtime systems.  The  preced‐
96       ing example then becomes:
97
98
99              #include <pthread.h>
100              static pthread_once_t random_is_initialized = PTHREAD_ONCE_INIT;
101              extern int initialize_random();
102
103
104              int random_function()
105              {
106                  (void) pthread_once(&random_is_initialized, initialize_random);
107                  ... /* Operations performed after initialization. */
108              }
109
110       Note that a pthread_once_t cannot be an array because some compilers do
111       not accept the construct &<array_name>.
112

FUTURE DIRECTIONS

114       None.
115

SEE ALSO

117       The Base Definitions volume of IEEE Std 1003.1-2001, <pthread.h>
118
120       Portions of this text are reprinted and reproduced in  electronic  form
121       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
122       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
123       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
124       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
125       event of any discrepancy between this version and the original IEEE and
126       The Open Group Standard, the original IEEE and The Open Group  Standard
127       is  the  referee document. The original Standard can be obtained online
128       at http://www.opengroup.org/unix/online.html .
129
130
131
132IEEE/The Open Group                  2003                     PTHREAD_ONCE(3P)
Impressum