Namespaces
Variants
Actions

call_once, once_flag, ONCE_FLAG_INIT

From cppreference.com
< c‎ | thread
Defined in header <threads.h>
void call_once( once_flag* flag, void (*func)(void) );
(1) (since C11)
typedef /* unspecified */ once_flag
(2) (since C11)
#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.

Contents

[edit] Parameters

flag - pointer to an object of type call_once that is used to ensure func is called only once
func - the function to execute only once

[edit] Return value

(none)

[edit] Notes

The POSIX equivalent of this function is pthread_once.

[edit] Example

#include <stdio.h>
#include <threads.h>
 
void do_once(void) {
    puts("called once");
}
 
static once_flag flag =