1PTHREAD_ONCE(3P) POSIX Programmer's Manual PTHREAD_ONCE(3P)
2
3
4
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
11
13 pthread_once — dynamic package initialization
14
16 #include <pthread.h>
17
18 int pthread_once(pthread_once_t *once_control,
19 void (*init_routine)(void));
20 pthread_once_t once_control = PTHREAD_ONCE_INIT;
21
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
40 Upon successful completion, pthread_once() shall return zero; other‐
41 wise, an error number shall be returned to indicate the error.
42
44 The pthread_once() function shall not return an error code of [EINTR].
45
46 The following sections are informative.
47
49 None.
50
52 None.
53
55 Some C libraries are designed for dynamic initialization. That is, the
56 global initialization for the library is performed when the first pro‐
57 cedure in the library is called. In a single-threaded program, this is
58 normally implemented using a static variable whose value is checked on
59 entry to a routine, as follows:
60
61 static int random_is_initialized = 0;
62 extern void initialize_random();
63
64 int random_function()
65 {
66 if (random_is_initialized == 0) {
67 initialize_random();
68 random_is_initialized = 1;
69 }
70 ... /* Operations performed after initialization. */
71 }
72
73 To keep the same structure in a multi-threaded program, a new primitive
74 is needed. Otherwise, library initialization has to be accomplished by
75 an explicit call to a library-exported initialization function prior to
76 any use of the library.
77
78 For dynamic library initialization in a multi-threaded process, a sim‐
79 ple initialization flag is not sufficient; the flag needs to be pro‐
80 tected against modification by multiple threads simultaneously calling
81 into the library. Protecting the flag requires the use of a mutex; how‐
82 ever, mutexes have to be initialized before they are used. Ensuring
83 that the mutex is only initialized once requires a recursive solution
84 to this problem.
85
86 The use of pthread_once() not only supplies an implementation-guaran‐
87 teed means of dynamic initialization, it provides an aid to the reli‐
88 able construction of multi-threaded and realtime systems. The preceding
89 example then becomes:
90
91 #include <pthread.h>
92 static pthread_once_t random_is_initialized = PTHREAD_ONCE_INIT;
93 extern void initialize_random();
94
95 int random_function()
96 {
97 (void) pthread_once(&random_is_initialized, initialize_random);
98 ... /* Operations performed after initialization. */
99 }
100
101 Note that a pthread_once_t cannot be an array because some compilers do
102 not accept the construct &<array_name>.
103
104 If an implementation detects that the value specified by the once_con‐
105 trol argument to pthread_once() does not refer to a pthread_once_t
106 object initialized by PTHREAD_ONCE_INIT, it is recommended that the
107 function should fail and report an [EINVAL] error.
108
110 None.
111
113 The Base Definitions volume of POSIX.1‐2008, <pthread.h>
114
116 Portions of this text are reprinted and reproduced in electronic form
117 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
118 -- Portable Operating System Interface (POSIX), The Open Group Base
119 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
120 cal and Electronics Engineers, Inc and The Open Group. (This is
121 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
122 event of any discrepancy between this version and the original IEEE and
123 The Open Group Standard, the original IEEE and The Open Group Standard
124 is the referee document. The original Standard can be obtained online
125 at http://www.unix.org/online.html .
126
127 Any typographical or formatting errors that appear in this page are
128 most likely to have been introduced during the conversion of the source
129 files to man page format. To report such errors, see https://www.ker‐
130 nel.org/doc/man-pages/reporting_bugs.html .
131
132
133
134IEEE/The Open Group 2013 PTHREAD_ONCE(3P)