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

NAME

6       pthread_once - dynamic package initialization
7

SYNOPSIS

9       #include <pthread.h>
10
11       int pthread_once(pthread_once_t *once_control,
12              void (*init_routine)(void));
13       pthread_once_t once_control = PTHREAD_ONCE_INIT;
14
15

DESCRIPTION

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

RETURN VALUE

34       Upon  successful  completion,  pthread_once() shall return zero; other‐
35       wise, an error number shall be returned to indicate the error.
36

ERRORS

38       The pthread_once() function may fail if:
39
40       EINVAL If either once_control or init_routine is invalid.
41
42
43       The pthread_once() function shall not return an error code of [EINTR].
44
45       The following sections are informative.
46

EXAMPLES

48       None.
49

APPLICATION USAGE

51       None.
52

RATIONALE

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

FUTURE DIRECTIONS

108       None.
109

SEE ALSO

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