For all invocations using a given once_flag, invokes a given fn exactly once across all threads. The first call to call_once() with a particular once_flag argument (that does not throw an exception) will run the specified function with the provided args; other calls with the same once_flag argument will not run the function, but will wait for the provided function to finish running (if it is still running).
Declared in <absl/base/call_once.h>
template<
typename Callable,
typename... Args>
void
call_once(
absl::once_flag& flag,
Callable&& fn,
Args&&... args);
This mechanism provides a safe, simple, and fast mechanism for one-time initialization in a multi-threaded process.
Example:
class MyInitClass { public: ... mutable absl::once_flag once_;
MyInitClass* init() const { absl::call_once(once_, &MyInitClass::Init, this); return ptr_; }
| Name | Description |
|---|---|
| flag | The once_flag used to distinguish this call from others. |
| fn | The callable to invoke at most once. |
| args | The arguments to pass to fn. |