liveBook · Manning

Call_once

call_once is a threading operation that coordinates callers so a specified callback is successfully executed at most once for a given once-control object.

How 3 Manning books explain it

Modern C, Third Edition

call_once is a C threads-library function that invokes a callback at most once for a specified once-control object, coordinating concurrent callers.

How it is used
Coordinates dynamic initialization that must become safe before concurrent threads access shared data.
How deep it goes
Explains the API, a shared-stream example, waiting behavior, and release-acquire visibility, but not callback failure handling.
Examples
Uses a lazily opened error log whose filename, stream, and buffering are initialized by one callback.
What you can do after
Apply call_once to publish a correctly initialized shared object across concurrent callers.

Read how Modern C, Third Edition explains call_once →

C++ Concurrency in Action, Second Edition

`std::call_once` is a C++ standard library function template that ensures a callable is executed exactly once for a given `std::once_flag`.

How it is used
Protects shared data during one-time initialization so callers see initialized state after call_once returns.
How deep it goes
Explains flag association, callable forms, synchronization, and resource and member initialization; defers detailed memory-model treatment.
Examples
Uses a shared resource pointer and a class connection initialized before send or receive operations.
What you can do after
Apply it via std::once_flag and std::call_once to initialize shared resources or class members safely.

Read how C++ Concurrency in Action, Second Edition explains call_once →

Modern C

call_once is a C threading function that invokes a specified function exactly once for a given once_flag, synchronizing concurrent callers until that invocation completes.

How it is used
Uses call_once as a last resort for dynamically initializing shared data before concurrent access.
How deep it goes
Explains the interface, callback selection, once_flag state, and waiting behavior, but does not develop implementation internals.
Examples
Demonstrates initializing a shared FILE stream with fopen, setvbuf, and an atexit cleanup callback.
What you can do after
Operate/configure a once_flag and callback so concurrent callers trigger shared initialization exactly once.

Read how Modern C explains call_once →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.