File: call_once.html

package info (click to toggle)
boost 1.27.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,908 kB
  • ctags: 26,546
  • sloc: cpp: 122,225; ansic: 10,956; python: 4,412; sh: 855; yacc: 803; makefile: 257; perl: 165; lex: 90; csh: 6
file content (132 lines) | stat: -rw-r--r-- 3,853 bytes parent folder | download
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
<html>
    <head>
        <meta http-equiv="Content-Type" content=
        "text/html; charset=iso-8859-1">
        <meta name="keywords" content=
        "threads, Boost.Threads, thread library, C++">
        <link rel="stylesheet" type="text/css" href="styles.css">

        <title>Boost.Threads, call_once</title>
    </head>

    <body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
        <table summary="header" border="0" cellpadding="7" cellspacing="0"
        width="100%">
            <tr>
                <td valign="top" width="300">
                    <h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
                    "277" height="86"></h3>
                </td>

                <td valign="top">
                    <h1 align="center">Boost.Threads</h1>

                    <h2 align="center">call_once</h2>
                </td>
            </tr>
        </table>
        <hr>

        <p><a href="#Introduction">Introduction</a><br>
         <a href="#Header">Header</a><br>
         <a href="#Synopsis">Synopsis</a><br>
         <a href="#Members">Members</a><br>
         <a href="#Example">Example</a></p>

        <h2><a name="Introduction">Introduction</a></h2>

        <p>The <code>call_once</code> routine and <code>once_flag</code> type
        can be used to run a routine exactly once. This can be used to
        initialize data in a <a href="definitions.html#Thread-safe">
        thread-safe</a> manner.</p>

        <h2><a name="Header">Header</a></h2>
<pre>
#include <a href=
"../../../boost/thread/once.hpp">&lt;boost/thread/once.hpp&gt;</a>
</pre>

        <h2><a name="Synopsis">Synopsis</a></h2>
<pre>
namespace boost {

typedef <i>[implementation defined]</i> once_flag;
const once_flag once_init = <i>[implementation defined]</i>;
void call_once(void (*func)(), once_flag&amp; flag);

} // namespace boost
</pre>

        <h2><a name="Reference">Reference</a></h2>
        <hr>

        <h3>once_flag</h3>

        <p>This implementation defined type is used as a flag to insure a
        routine is called only once. Instances of this type should be
        statically initialized to <code>once_init</code>.</p>
        <hr>

        <h3>once_init</h3>

        <p>This is a constant value used to initialize <code>once_flag</code>
        instances to indicate that the logically associated routine has not
        been run yet.</p>
        <hr>

        <h3>call_once</h3>
<pre>
   void call_once(void (*func)(), once_flag&amp; flag);
</pre>

        <p><b>Requires:</b> The function <code>func</code> shall not throw
        exceptions.</p>

        <p><b>Effects:</b> As if (in an atomic fashion)</p>
        <code>&nbsp;&nbsp;&nbsp;if (flag == once_init)<br>
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;func();</code> 

        <p><b>Postcondition:</b> <code>flag</code> != <code>
        once_init</code></p>
        <hr>

        <h2><a name="Example">Example Usage</a></h2>
<pre>
#include <a href=
"../../../boost/thread/thread.hpp">&lt;boost/thread/thread.hpp&gt;</a>
#include <a href=
"../../../boost/thread/tss.hpp">&lt;boost/thread/once.hpp&gt;</a>
#include &lt;cassert&gt;

int value=0;
boost::once_flag once = boost::once_init;

void init()
{
    ++value;
}

void thread_proc()
{
   boost::call_once(&amp;init, once);
}

int main(int argc, char* argv[])
{
   boost::thread_group threads;
   for (int i=0; i&lt;5; ++i)
      threads.create_thread(&amp;thread_proc);
   threads.join_all();
   assert(value == 1);
}
</pre>
        <hr>

        <p>Revised 
        <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>

        <p><i>&copy; Copyright <a href="mailto:williamkempf@hotmail.com">
        William E. Kempf</a> 2001 all rights reserved.</i></p>
    </body>
</html>