mirror of
https://github.com/boostorg/thread.git
synced 2026-01-22 17:52:18 +00:00
117 lines
4.2 KiB
HTML
117 lines
4.2 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<link rel="stylesheet" type="text/css" href="../../../boost.css">
|
|
<title>Boost.Threads - Header <boost/thread/once.hpp></title>
|
|
</head>
|
|
<body link="#0000ff" vlink="#800080">
|
|
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
|
|
"header">
|
|
<tr>
|
|
<td valign="top" width="300">
|
|
<h3><a href="../../../index.htm"><img height="86" width="277" alt="C++ Boost" src="../../../c++boost.gif" border="0"></a></h3>
|
|
</td>
|
|
<td valign="top">
|
|
<h1 align="center">Boost.Threads</h1>
|
|
<h2 align="center">Header <<a href="../../../boost/thread/once.hpp">boost/thread/once.hpp</a>></h2>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
<hr>
|
|
<h2>Contents</h2>
|
|
<dl class="page-index">
|
|
<dt><a href="#introduction">Introduction</a></dt>
|
|
<dt><a href="#macros">Macros</a></dt>
|
|
<dl class="page-index">
|
|
<dt><a href="#macro-BOOST_ONCE_INIT">BOOST_ONCE_INIT</a></dt>
|
|
</dl>
|
|
<dt><a href="#types">Types</a></dt>
|
|
<dl class="page-index">
|
|
<dt><a href="#type-once_flag">once_flag</a></dt>
|
|
</dl>
|
|
<dt><a href="#functions">Functions</a></dt>
|
|
<dl class="page-index">
|
|
<dt><a href="#function-call_once">call_once</a></dt>
|
|
</dl>
|
|
<dt><a href="#examples">Example(s)</a></dt>
|
|
</dl>
|
|
<hr>
|
|
<h2><a name="introduction"></a>Introduction</h2>
|
|
<p>Include the header <<a href="../../../boost/thread/once.hpp">boost/thread/once.hpp</a>>
|
|
to define the <code>call_once</code> function, <code>once_flag</code> type and
|
|
<code>BOOST_ONCE_INIT</code> constant.</p>
|
|
<p>The <code>call_once</code> function and <code>once_flag</code> type (statically
|
|
initialized to <code>BOOST_ONCE_INIT</code>) 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="macros"></a>Macros</h2>
|
|
<pre>
|
|
<a name="macro-BOOST_ONCE_INIT"></a>#define BOOST_ONCE_INIT <i>implementation defined</i>
|
|
</pre>
|
|
<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>
|
|
<h2><a name="types"></a>Types</h2>
|
|
<pre>
|
|
<a name="type-once_flag"></a>typedef <i>implementation defined</i> once_flag;
|
|
</pre>
|
|
<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>BOOST_ONCE_INIT</code>.</p>
|
|
<h2><a name="functions"></a>Functions</h2>
|
|
<pre>
|
|
<a name="function-call_once"></a>void call_once(void (*func)(), once_flag& flag);
|
|
</pre>
|
|
<dl class="function-semantics">
|
|
<dt><b>Requires:</b> The function <code>func</code> shall not throw exceptions.</dt>
|
|
<dt><b>Effects:</b> As if (in an atomic fashion):
|
|
<pre>
|
|
if (flag == BOOST_ONCE_INIT)
|
|
func();
|
|
</pre>
|
|
</dt>
|
|
<dt><b>Postconditions:</b> <code>flag != BOOST_ONCE_INIT</code></dt>
|
|
</dl>
|
|
<h2><a name="examples"></a>Example(s)</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 -->
|
|
05 November, 2001
|
|
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
|
|
</p>
|
|
<p><i>© Copyright <a href="mailto:wekempf@cox.net">William E. Kempf</a> 2001-2002.
|
|
All Rights Reserved.</i></p>
|
|
<p>Permission to use, copy, modify, distribute and sell this software and its
|
|
documentation for any purpose is hereby granted without fee, provided that the
|
|
above copyright notice appear in all copies and that both that copyright notice
|
|
and this permission notice appear in supporting documentation. William E. Kempf
|
|
makes no representations about the suitability of this software for any purpose.
|
|
It is provided "as is" without express or implied warranty.</p>
|
|
</body>
|
|
</html>
|