diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/thread%2Fcall_once.html | |
new repository
Diffstat (limited to 'devdocs/c/thread%2Fcall_once.html')
| -rw-r--r-- | devdocs/c/thread%2Fcall_once.html | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/devdocs/c/thread%2Fcall_once.html b/devdocs/c/thread%2Fcall_once.html new file mode 100644 index 00000000..c49a1c35 --- /dev/null +++ b/devdocs/c/thread%2Fcall_once.html @@ -0,0 +1,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><threads.h></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 <stdio.h> +#include <threads.h> + +void do_once(void) { + puts("called once"); +} + +static once_flag flag = ONCE_FLAG_INIT; +int func(void* data) +{ + call_once(&flag, do_once); +} + +int main(void) +{ + thrd_t t1, t2, t3, t4; + thrd_create(&t1, func, NULL); + thrd_create(&t2, func, NULL); + thrd_create(&t3, func, NULL); + thrd_create(&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"> + © 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> |
