Overview
std.once
Comprehensive reference for Zig's std.once module covering low-level systems primitives and metaprogramming utilities.
This page syncs automatically from Zig's source:
std/once.md.Zig Standard Library Documentation
| Key | Value |
|---|---|
| Module | std.once |
| Declarations | 2 |
| Breakdown | 2 functions |
| Generated (unix epoch) | 1760148108 |
Table of Contents
Functions (2)
once
Function – Expand to view signature, parameters, and examples.
pub fn once(comptime f: fn () void) Once(f) {
return Once(f){};
}
Parameters & Return:
| Name | Type | Description | Default |
|---|---|---|---|
f | fn () void | – | – |
| Return | Once(f) | – | – |
Once
Function – An object that executes the function `f` just once
An object that executes the function f just once.
It is undefined behavior if f re-enters the same Once instance.
pub fn Once(comptime f: fn () void) type {
return struct {
done: bool = false,
mutex: std.Thread.Mutex = std.Thread.Mutex{},
/// Call the function `f`.
/// If `call` is invoked multiple times `f` will be executed only the
/// first time.
/// The invocations are thread-safe.
pub fn call(self: *@This()) void {
if (@atomicLoad(bool, &self.done, .acquire))
return;
return self.callSlow();
}
fn callSlow(self: *@This()) void {
@branchHint(.cold);
self.mutex.lock();
defer self.mutex.unlock();
// The first thread to acquire the mutex gets to run the initializer
if (!self.done) {
f();
@atomicStore(bool, &self.done, true, .release);
}
}
};
}
Parameters & Return:
| Name | Type | Description | Default |
|---|---|---|---|
f | fn () void | – | – |
| Return | type | – | – |