mirror of
https://github.com/boostorg/thread.git
synced 2026-01-24 06:22:12 +00:00
129 lines
3.2 KiB
HTML
129 lines
3.2 KiB
HTML
<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 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"><boost/thread/once.hpp></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& 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& 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>
|
|
if (flag == once_init)<br>
|
|
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"><boost/thread/thread.hpp></a>
|
|
#include <a href="../../../boost/thread/tss.hpp"><boost/thread/once.hpp></a>
|
|
#include <cassert>
|
|
|
|
int value=0;
|
|
boost::once_flag once = boost::once_init;
|
|
|
|
void init()
|
|
{
|
|
++value;
|
|
}
|
|
|
|
void thread_proc()
|
|
{
|
|
boost::call_once(&init, once);
|
|
}
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
boost::thread_group threads;
|
|
for (int i=0; i<5; ++i)
|
|
threads.create_thread(&thread_proc);
|
|
threads.join_all();
|
|
assert(value == 1);
|
|
}
|
|
</pre>
|
|
|
|
<hr>
|
|
|
|
<p>Revised <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->13 September, 2001<!--webbot bot="Timestamp" endspan i-checksum="39334" -->
|
|
</p>
|
|
|
|
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">William E. Kempf</a>
|
|
2001 all rights reserved.</i></p>
|
|
|
|
</body>
|
|
</html>
|