call_once, once_flag, ONCE_FLAG_INIT
Header: <threads.h>
- 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.
# Declarations
void call_once( once_flag* flag, void (*func)(void) );
(since C11)
typedef /* unspecified */ once_flag
(since C11)
#define ONCE_FLAG_INIT /* unspecified */
(since C11)
# Parameters
flag: pointer to an object of type call_once that is used to ensure func is called only oncefunc: the function to execute only once
# Return value
(none)
# Notes
The POSIX equivalent of this function is pthread_once.
# Example
#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);
}