#define ONCE_FLAG_INIT /* unspecified */
(3) (since C11)1) Calls function func
exactly once, even if invoked from several threads. The completion of the function func
synchronizes with all previous or subsequent calls to call_once
with the same flag
variable.
2) Complete object type capable of holding a flag used by call_once
.
3) Expands to a value that can be used to initialize an object of type once_flag
.
call_once
that is used to ensure func
is called only once func - the function to execute only once [edit] Return value
(none)
[edit] NotesThe POSIX equivalent of this function is pthread_once
.
#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); }
Output:
[edit] ReferencesRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4