summaryrefslogtreecommitdiff
path: root/devdocs/c/thread%2Fcall_once.html
blob: c49a1c35046806e211f83e81bb589de307ff466a (plain)
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
    <h1 id="firstHeading" class="firstHeading">call_once, once_flag, ONCE_FLAG_INIT</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;threads.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">void call_once( once_flag* flag, void (*func)(void) );</pre>
</td> <td> (1) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">typedef /* unspecified */ once_flag</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">#define ONCE_FLAG_INIT /* unspecified */</pre>
</td> <td> (3) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr>  </table> <div class="t-li1">
<span class="t-li">1)</span> Calls function <code>func</code> exactly once, even if invoked from several threads. The completion of the function <code>func</code> synchronizes with all previous or subsequent calls to <code>call_once</code> with the same <code>flag</code> variable.</div> <div class="t-li1">
<span class="t-li">2)</span> Complete object type capable of holding a flag used by <code>call_once</code>.</div> <div class="t-li1">
<span class="t-li">3)</span> Expands to a value that can be used to initialize an object of type <code>once_flag</code>.</div>  <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> flag </td> <td> - </td> <td> pointer to an object of type <code>call_once</code> that is used to ensure <code>func</code> is called only once </td>
</tr> <tr class="t-par"> <td> func </td> <td> - </td> <td> the function to execute only once </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>(none)</p>
<h3 id="Notes"> Notes</h3> <p>The POSIX equivalent of this function is <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_once.html"><code>pthread_once</code></a>.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;threads.h&gt;
 
void do_once(void) {
    puts("called once");
}
 
static once_flag flag = ONCE_FLAG_INIT;
int func(void* data)
{
    call_once(&amp;flag, do_once);
}
 
int main(void)
{
    thrd_t t1, t2, t3, t4;
    thrd_create(&amp;t1, func, NULL);
    thrd_create(&amp;t2, func, NULL);
    thrd_create(&amp;t3, func, NULL);
    thrd_create(&amp;t4, func, NULL);
 
    thrd_join(t1, NULL);
    thrd_join(t2, NULL);
    thrd_join(t3, NULL);
    thrd_join(t4, NULL);
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">called once</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.26.2.1 The call_once function (p: 275) </li>
<li> 7.26.1/3 ONCE_FLAG_INIT (p: 274) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.26.2.1 The call_once function (p: 378) </li>
<li> 7.26.1/3 ONCE_FLAG_INIT (p: 376) </li>
</ul>
</ul>         <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/thread/call_once" title="cpp/thread/call once">C++ documentation</a></span> for <code>call_once</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/thread/call_once" class="_attribution-link">https://en.cppreference.com/w/c/thread/call_once</a>
  </p>
</div>