Files
hustoj/web/cppreference-zh/chmhelp/c-thread-once_flag_init.html
2025-08-01 12:57:54 +08:00

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">&lt; <a href="c.html">c</a>&lrm; | <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>&#23450;&#20041;&#20110;&#22836;&#25991;&#20214; <code>&lt;threads.h&gt;</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 &#36215;)</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 &#36215;)</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 &#36215;)</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> &#20934;&#30830;&#35843;&#29992;&#20989;&#25968; <code>func</code> &#19968;&#27425;&#65292;&#21363;&#20351;&#20174;&#22810;&#20010;&#32447;&#31243;&#35843;&#29992;&#12290;&#20989;&#25968; <code>func</code> &#30340;&#23436;&#25104;&#19982;&#20808;&#21069;&#25110;&#21518;&#32487;&#30340;&#29992;&#21516;&#19968; <code>flag</code> &#23545;&#35937;&#30340;&#23545; <code>call_once</code> &#35843;&#29992;&#21516;&#27493;&#12290;</div>
<div class="t-li1"><span class="t-li">2)</span> &#36275;&#20197;&#20445;&#26377; <code>call_once</code> &#25152;&#29992;&#26631;&#24535;&#30340;&#23436;&#25972;&#23545;&#35937;&#31867;&#22411;&#12290;</div>
<div class="t-li1"><span class="t-li">3)</span> &#23637;&#24320;&#25104;&#33021;&#29992;&#20110;&#21021;&#22987;&#21270; <code>once_flag</code> &#31867;&#22411;&#23545;&#35937;&#30340;&#20540;&#12290;</div>
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">&#21442;&#25968;</span></h3>
<table class="t-par-begin">
<tr class="t-par">
<td>flag</td>
<td>-</td>
<td>&#25351;&#21521;&#29992;&#20110;&#30830;&#20445;&#21482;&#35843;&#29992;&#19968;&#27425; <code>func</code> &#30340; <code>call_once</code> &#23545;&#35937;&#30340;&#25351;&#38024;</td>
</tr>
<tr class="t-par">
<td>func</td>
<td>-</td>
<td>&#21482;&#25191;&#34892;&#19968;&#27425;&#30340;&#20989;&#25968;</td>
</tr>
</table>
<h3><span class="mw-headline" id=".E8.BF.94.E5.9B.9E.E5.80.BC">&#36820;&#22238;&#20540;</span></h3>
<p>&#65288;&#26080;&#65289;</p>
<h3><span class="mw-headline" id=".E6.B3.A8.E6.84.8F">&#27880;&#24847;</span></h3>
<p>&#27492;&#20989;&#25968;&#30340; POSIX &#31561;&#20215;&#29289;&#26159; <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_once.html">pthread_once</a> &#12290;</p>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">&#31034;&#20363;</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 &lt;stdio.h&gt;</span>
<span class="co2">#include &lt;threads.h&gt;</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">&quot;called once&quot;</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">&amp;</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">&amp;</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">&amp;</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">&amp;</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">&amp;</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>&#36755;&#20986;&#65306;</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">&#24341;&#29992;</span></h3>
<div class="t-ref-std-11">
<ul>
<li>C11 &#26631;&#20934;&#65288;ISO/IEC 9899:2011&#65289;&#65306;</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">&#21442;&#38405;</span></h3>
<table class="t-dsc-begin"></table>
</div>
<div class="visualClear"></div>
</div>
</div>
</div>
</body>
</html>