From N3220, §7.28.2.1 “The call_once function”:
Synopsis
#include <threads.h> void call_once(once_flag *flag, void (*func)(void));Description
The
call_oncefunction uses theonce_flagpointed to by flag to ensure thatfuncis called exactly once, the first time thecall_oncefunction is called with that value offlag. Completion of an effective call to thecall_oncefunction synchronizes with all subsequent calls to thecall_oncefunction with the same value of flag.Returns
The
call_oncefunction returns no value.
From what it sounds like, is this supposed to be used for init()/config() functions that are only supposed to be called once? Or am I completely misunderstanding it and it is something pertinent to threads?
What is this useful for? What is flag, and how is it relevant here?
Let's assume the following:
call_once.If those assumptions are met, the following two guarantees are made for any function provided as an argument:
call_once will only return once the function has returned.The second guarantee means that other threads will block until the provided function returns if it's currently executing.
This is useful for initializing/populating shared variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With