140 lines
8.2 KiB
HTML
140 lines
8.2 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh" dir="ltr" class="client-nojs" xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh">
|
|
<head><meta http-equiv="x-ua-compatible" content="ie=edge">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<title>call_once, once_flag, ONCE_FLAG_INIT</title>
|
|
<link rel="stylesheet" href="ext.css" />
|
|
<link rel="stylesheet" href="site_modules.css" />
|
|
</head>
|
|
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-c_thread_call_once skin-cppreference2 action-view cpp-navbar">
|
|
<div id="cpp-content-base">
|
|
<div id="content"><a id="top"></a>
|
|
<h1 id="firstHeading" class="firstHeading">call_once, once_flag, ONCE_FLAG_INIT</h1>
|
|
<div id="bodyContent">
|
|
<div id="contentSub"><span class="subpages">< <a href="c.html">c</a>‎ | <a href="c-thread.html">thread</a></span></div>
|
|
<div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr" xml:lang="zh">
|
|
<table class="t-dcl-begin">
|
|
<tbody>
|
|
<tr class="t-dsc-header">
|
|
<td>
|
|
<div>定义于头文件 <code><threads.h></code></div>
|
|
</td>
|
|
<td></td>
|
|
<td></td>
|
|
</tr>
|
|
<tr class="t-dcl t-since-c11">
|
|
<td>
|
|
<div><span class="mw-geshi c source-c"><span class="kw4">void</span> call_once<span class="br0">(</span> <a href="c-thread.html"><span class="kw1022">once_flag</span></a><span class="sy2">*</span> flag, <span class="kw4">void</span> <span class="br0">(</span><span class="sy2">*</span>func<span class="br0">)</span><span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span> <span class="br0">)</span><span class="sy4">;</span></span></div>
|
|
</td>
|
|
<td>(1)</td>
|
|
<td><span class="t-mark-rev t-since-c11">(C11 起)</span></td>
|
|
</tr>
|
|
<tr class="t-dcl t-since-c11">
|
|
<td>
|
|
<div><span class="mw-geshi c source-c"><span class="kw1">typedef</span> <span class="coMULTI">/* unspecified */</span> <a href="c-thread.html"><span class="kw1022">once_flag</span></a></span></div>
|
|
</td>
|
|
<td>(2)</td>
|
|
<td><span class="t-mark-rev t-since-c11">(C11 起)</span></td>
|
|
</tr>
|
|
<tr class="t-dcl t-since-c11">
|
|
<td>
|
|
<div><span class="mw-geshi c source-c"><span class="co2">#define ONCE_FLAG_INIT /* unspecified */</span></span></div>
|
|
</td>
|
|
<td>(3)</td>
|
|
<td><span class="t-mark-rev t-since-c11">(C11 起)</span></td>
|
|
</tr>
|
|
<tr class="t-dcl-sep">
|
|
<td></td>
|
|
<td></td>
|
|
<td></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<div class="t-li1"><span class="t-li">1)</span> 准确调用函数 <code>func</code> 一次,即使从多个线程调用。函数 <code>func</code> 的完成与先前或后继的用同一 <code>flag</code> 对象的对 <code>call_once</code> 调用同步。</div>
|
|
<div class="t-li1"><span class="t-li">2)</span> 足以保有 <code>call_once</code> 所用标志的完整对象类型。</div>
|
|
<div class="t-li1"><span class="t-li">3)</span> 展开成能用于初始化 <code>once_flag</code> 类型对象的值。</div>
|
|
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">参数</span></h3>
|
|
<table class="t-par-begin">
|
|
<tr class="t-par">
|
|
<td>flag</td>
|
|
<td>-</td>
|
|
<td>指向用于确保只调用一次 <code>func</code> 的 <code>call_once</code> 对象的指针</td>
|
|
</tr>
|
|
<tr class="t-par">
|
|
<td>func</td>
|
|
<td>-</td>
|
|
<td>只执行一次的函数</td>
|
|
</tr>
|
|
</table>
|
|
<h3><span class="mw-headline" id=".E8.BF.94.E5.9B.9E.E5.80.BC">返回值</span></h3>
|
|
<p>(无)</p>
|
|
<h3><span class="mw-headline" id=".E6.B3.A8.E6.84.8F">注意</span></h3>
|
|
<p>此函数的 POSIX 等价物是 <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_once.html">pthread_once</a> 。</p>
|
|
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">示例</span></h3>
|
|
<div class="t-example">
|
|
<div class="t-example-live-link"></div>
|
|
<div dir="ltr" class="mw-geshi" style="text-align: left;">
|
|
<div class="c source-c">
|
|
<pre class="de1"><span class="co2">#include <stdio.h></span>
|
|
<span class="co2">#include <threads.h></span>
|
|
|
|
<span class="kw4">void</span> do_once<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span> <span class="br0">{</span>
|
|
<a href="c-io-puts.html"><span class="kw836">puts</span></a><span class="br0">(</span><span class="st0">"called once"</span><span class="br0">)</span><span class="sy4">;</span>
|
|
<span class="br0">}</span>
|
|
|
|
<span class="kw4">static</span> <a href="c-thread.html"><span class="kw1022">once_flag</span></a> flag <span class="sy1">=</span> <a href="c-thread-ONCE_FLAG_INIT.html"><span class="kw1023">ONCE_FLAG_INIT</span></a><span class="sy4">;</span>
|
|
<span class="kw4">int</span> func<span class="br0">(</span><span class="kw4">void</span><span class="sy2">*</span> data<span class="br0">)</span>
|
|
<span class="br0">{</span>
|
|
call_once<span class="br0">(</span><span class="sy3">&</span>flag, do_once<span class="br0">)</span><span class="sy4">;</span>
|
|
<span class="br0">}</span>
|
|
|
|
<span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span>
|
|
<span class="br0">{</span>
|
|
<a href="c-thread.html"><span class="kw996">thrd_t</span></a> t1, t2, t3, t4<span class="sy4">;</span>
|
|
<a href="c-thread-thrd_create.html"><span class="kw997">thrd_create</span></a><span class="br0">(</span><span class="sy3">&</span>t1, func, <a href="c-types-NULL.html"><span class="kw103">NULL</span></a><span class="br0">)</span><span class="sy4">;</span>
|
|
<a href="c-thread-thrd_create.html"><span class="kw997">thrd_create</span></a><span class="br0">(</span><span class="sy3">&</span>t2, func, <a href="c-types-NULL.html"><span class="kw103">NULL</span></a><span class="br0">)</span><span class="sy4">;</span>
|
|
<a href="c-thread-thrd_create.html"><span class="kw997">thrd_create</span></a><span class="br0">(</span><span class="sy3">&</span>t3, func, <a href="c-types-NULL.html"><span class="kw103">NULL</span></a><span class="br0">)</span><span class="sy4">;</span>
|
|
<a href="c-thread-thrd_create.html"><span class="kw997">thrd_create</span></a><span class="br0">(</span><span class="sy3">&</span>t4, func, <a href="c-types-NULL.html"><span class="kw103">NULL</span></a><span class="br0">)</span><span class="sy4">;</span>
|
|
|
|
<a href="c-thread-thrd_join.html"><span class="kw1004">thrd_join</span></a><span class="br0">(</span>t1, <a href="c-types-NULL.html"><span class="kw103">NULL</span></a><span class="br0">)</span><span class="sy4">;</span>
|
|
<a href="c-thread-thrd_join.html"><span class="kw1004">thrd_join</span></a><span class="br0">(</span>t2, <a href="c-types-NULL.html"><span class="kw103">NULL</span></a><span class="br0">)</span><span class="sy4">;</span>
|
|
<a href="c-thread-thrd_join.html"><span class="kw1004">thrd_join</span></a><span class="br0">(</span>t3, <a href="c-types-NULL.html"><span class="kw103">NULL</span></a><span class="br0">)</span><span class="sy4">;</span>
|
|
<a href="c-thread-thrd_join.html"><span class="kw1004">thrd_join</span></a><span class="br0">(</span>t4, <a href="c-types-NULL.html"><span class="kw103">NULL</span></a><span class="br0">)</span><span class="sy4">;</span>
|
|
<span class="br0">}</span></pre></div>
|
|
</div>
|
|
<p>输出:</p>
|
|
<div dir="ltr" class="mw-geshi" style="text-align: left;">
|
|
<div class="text source-text">
|
|
<pre class="de1">called once</pre></div>
|
|
</div>
|
|
</div>
|
|
<h3><span class="mw-headline" id=".E5.BC.95.E7.94.A8">引用</span></h3>
|
|
<div class="t-ref-std-11">
|
|
<ul>
|
|
<li>C11 标准(ISO/IEC 9899:2011):</li>
|
|
</ul>
|
|
<dl>
|
|
<dd>
|
|
<ul>
|
|
<li>7.26.2.1 The call_once function (p: 378)</li>
|
|
</ul>
|
|
</dd>
|
|
</dl>
|
|
<dl>
|
|
<dd>
|
|
<ul>
|
|
<li>7.26.1/3 ONCE_FLAG_INIT (p: 376)</li>
|
|
</ul>
|
|
</dd>
|
|
</dl>
|
|
</div>
|
|
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
|
|
<table class="t-dsc-begin"></table>
|
|
</div>
|
|
<div class="visualClear"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|