~ubuntu-branches/ubuntu/precise/manpages-posix/precise

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
.\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved 
.TH "PTHREAD_ONCE" P 2003 "IEEE/The Open Group" "POSIX Programmer's Manual"
.\" pthread_once 
.SH NAME
pthread_once \- dynamic package initialization
.SH SYNOPSIS
.LP
\fB#include <pthread.h>
.br
.sp
int pthread_once(pthread_once_t *\fP\fIonce_control\fP\fB,
.br
\ \ \ \ \ \  void (*\fP\fIinit_routine\fP\fB)(void));
.br
pthread_once_t\fP \fIonce_control\fP \fB= PTHREAD_ONCE_INIT; \fP
\fB
.br
\fP
.SH DESCRIPTION
.LP
The first call to \fIpthread_once\fP() by any thread in a process,
with a given \fIonce_control\fP, shall call the
\fIinit_routine\fP with no arguments. Subsequent calls of \fIpthread_once\fP()
with the same \fIonce_control\fP shall not call
the \fIinit_routine\fP. On return from \fIpthread_once\fP(), \fIinit_routine\fP
shall have completed. The \fIonce_control\fP
parameter shall determine whether the associated initialization routine
has been called.
.LP
The \fIpthread_once\fP() function is not a cancellation point. However,
if \fIinit_routine\fP is a cancellation point and is
canceled, the effect on \fIonce_control\fP shall be as if \fIpthread_once\fP()
was never called.
.LP
The constant PTHREAD_ONCE_INIT is defined in the \fI<pthread.h>\fP
header.
.LP
The behavior of \fIpthread_once\fP() is undefined if \fIonce_control\fP
has automatic storage duration or is not initialized
by PTHREAD_ONCE_INIT.
.SH RETURN VALUE
.LP
Upon successful completion, \fIpthread_once\fP() shall return zero;
otherwise, an error number shall be returned to indicate
the error.
.SH ERRORS
.LP
The \fIpthread_once\fP() function may fail if:
.TP 7
.B EINVAL
If either \fIonce_control\fP or \fIinit_routine\fP is invalid.
.sp
.LP
The \fIpthread_once\fP() function shall not return an error code of
[EINTR].
.LP
\fIThe following sections are informative.\fP
.SH EXAMPLES
.LP
None.
.SH APPLICATION USAGE
.LP
None.
.SH RATIONALE
.LP
Some C libraries are designed for dynamic initialization. That is,
the global initialization for the library is performed when
the first procedure in the library is called. In a single-threaded
program, this is normally implemented using a static variable
whose value is checked on entry to a routine, as follows:
.sp
.RS
.nf

\fBstatic int random_is_initialized = 0;
extern int initialize_random();
.sp

int random_function()
{
    if (random_is_initialized == 0) {
        initialize_random();
        random_is_initialized = 1;
    }
    ... /* Operations performed after initialization. */
}
\fP
.fi
.RE
.LP
To keep the same structure in a multi-threaded program, a new primitive
is needed. Otherwise, library initialization has to be
accomplished by an explicit call to a library-exported initialization
function prior to any use of the library.
.LP
For dynamic library initialization in a multi-threaded process, a
simple initialization flag is not sufficient; the flag needs
to be protected against modification by multiple threads simultaneously
calling into the library. Protecting the flag requires the
use of a mutex; however, mutexes have to be initialized before they
are used. Ensuring that the mutex is only initialized once
requires a recursive solution to this problem.
.LP
The use of \fIpthread_once\fP() not only supplies an implementation-guaranteed
means of dynamic initialization, it provides an
aid to the reliable construction of multi-threaded and realtime systems.
The preceding example then becomes:
.sp
.RS
.nf

\fB#include <pthread.h>
static pthread_once_t random_is_initialized = PTHREAD_ONCE_INIT;
extern int initialize_random();
.sp

int random_function()
{
    (void) pthread_once(&random_is_initialized, initialize_random);
    ... /* Operations performed after initialization. */
}
\fP
.fi
.RE
.LP
Note that a \fBpthread_once_t\fP cannot be an array because some compilers
do not accept the construct
\fB&<array_name>\fP.
.SH FUTURE DIRECTIONS
.LP
None.
.SH SEE ALSO
.LP
The Base Definitions volume of IEEE\ Std\ 1003.1-2001, \fI<pthread.h>\fP
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group. In the
event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.opengroup.org/unix/online.html .