]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3p/pthread_once.3p
Import of man-pages 1.70
[thirdparty/man-pages.git] / man3p / pthread_once.3p
1 .\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved
2 .TH "PTHREAD_ONCE" P 2003 "IEEE/The Open Group" "POSIX Programmer's Manual"
3 .\" pthread_once
4 .SH NAME
5 pthread_once \- dynamic package initialization
6 .SH SYNOPSIS
7 .LP
8 \fB#include <pthread.h>
9 .br
10 .sp
11 int pthread_once(pthread_once_t *\fP\fIonce_control\fP\fB,
12 .br
13 \ \ \ \ \ \ void (*\fP\fIinit_routine\fP\fB)(void));
14 .br
15 pthread_once_t\fP \fIonce_control\fP \fB= PTHREAD_ONCE_INIT; \fP
16 \fB
17 .br
18 \fP
19 .SH DESCRIPTION
20 .LP
21 The first call to \fIpthread_once\fP() by any thread in a process,
22 with a given \fIonce_control\fP, shall call the
23 \fIinit_routine\fP with no arguments. Subsequent calls of \fIpthread_once\fP()
24 with the same \fIonce_control\fP shall not call
25 the \fIinit_routine\fP. On return from \fIpthread_once\fP(), \fIinit_routine\fP
26 shall have completed. The \fIonce_control\fP
27 parameter shall determine whether the associated initialization routine
28 has been called.
29 .LP
30 The \fIpthread_once\fP() function is not a cancellation point. However,
31 if \fIinit_routine\fP is a cancellation point and is
32 canceled, the effect on \fIonce_control\fP shall be as if \fIpthread_once\fP()
33 was never called.
34 .LP
35 The constant PTHREAD_ONCE_INIT is defined in the \fI<pthread.h>\fP
36 header.
37 .LP
38 The behavior of \fIpthread_once\fP() is undefined if \fIonce_control\fP
39 has automatic storage duration or is not initialized
40 by PTHREAD_ONCE_INIT.
41 .SH RETURN VALUE
42 .LP
43 Upon successful completion, \fIpthread_once\fP() shall return zero;
44 otherwise, an error number shall be returned to indicate
45 the error.
46 .SH ERRORS
47 .LP
48 The \fIpthread_once\fP() function may fail if:
49 .TP 7
50 .B EINVAL
51 If either \fIonce_control\fP or \fIinit_routine\fP is invalid.
52 .sp
53 .LP
54 The \fIpthread_once\fP() function shall not return an error code of
55 [EINTR].
56 .LP
57 \fIThe following sections are informative.\fP
58 .SH EXAMPLES
59 .LP
60 None.
61 .SH APPLICATION USAGE
62 .LP
63 None.
64 .SH RATIONALE
65 .LP
66 Some C libraries are designed for dynamic initialization. That is,
67 the global initialization for the library is performed when
68 the first procedure in the library is called. In a single-threaded
69 program, this is normally implemented using a static variable
70 whose value is checked on entry to a routine, as follows:
71 .sp
72 .RS
73 .nf
74
75 \fBstatic int random_is_initialized = 0;
76 extern int initialize_random();
77 .sp
78
79 int random_function()
80 {
81 if (random_is_initialized == 0) {
82 initialize_random();
83 random_is_initialized = 1;
84 }
85 ... /* Operations performed after initialization. */
86 }
87 \fP
88 .fi
89 .RE
90 .LP
91 To keep the same structure in a multi-threaded program, a new primitive
92 is needed. Otherwise, library initialization has to be
93 accomplished by an explicit call to a library-exported initialization
94 function prior to any use of the library.
95 .LP
96 For dynamic library initialization in a multi-threaded process, a
97 simple initialization flag is not sufficient; the flag needs
98 to be protected against modification by multiple threads simultaneously
99 calling into the library. Protecting the flag requires the
100 use of a mutex; however, mutexes have to be initialized before they
101 are used. Ensuring that the mutex is only initialized once
102 requires a recursive solution to this problem.
103 .LP
104 The use of \fIpthread_once\fP() not only supplies an implementation-guaranteed
105 means of dynamic initialization, it provides an
106 aid to the reliable construction of multi-threaded and realtime systems.
107 The preceding example then becomes:
108 .sp
109 .RS
110 .nf
111
112 \fB#include <pthread.h>
113 static pthread_once_t random_is_initialized = PTHREAD_ONCE_INIT;
114 extern int initialize_random();
115 .sp
116
117 int random_function()
118 {
119 (void) pthread_once(&random_is_initialized, initialize_random);
120 ... /* Operations performed after initialization. */
121 }
122 \fP
123 .fi
124 .RE
125 .LP
126 Note that a \fBpthread_once_t\fP cannot be an array because some compilers
127 do not accept the construct
128 \fB&<array_name>\fP.
129 .SH FUTURE DIRECTIONS
130 .LP
131 None.
132 .SH SEE ALSO
133 .LP
134 The Base Definitions volume of IEEE\ Std\ 1003.1-2001, \fI<pthread.h>\fP
135 .SH COPYRIGHT
136 Portions of this text are reprinted and reproduced in electronic form
137 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
138 -- Portable Operating System Interface (POSIX), The Open Group Base
139 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
140 Electrical and Electronics Engineers, Inc and The Open Group. In the
141 event of any discrepancy between this version and the original IEEE and
142 The Open Group Standard, the original IEEE and The Open Group Standard
143 is the referee document. The original Standard can be obtained online at
144 http://www.opengroup.org/unix/online.html .