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.
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.
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.
What to know first
- command-line argumentA command-line argument is a value or option supplied to a program when it is invoked, providing input or affecting its behavior.
- static storageStatic storage duration is the storage duration of an object whose lifetime extends throughout program execution.
Related concepts
- std::once_flaguses · A once_flag is a synchronization object that coordinates threads to ensure a one-time function invocation occurs only once.
- callbackuses · A callback is a function or other callable supplied or registered with a component for invocation at a specified event, condition, or point in execution.
- atexitcontrasts with · atexit is a C standard-library function that registers a function to execute during normal program termination.
- condition variablealternative to · A condition variable is a synchronization object that allows threads to wait for a shared-state condition and be notified when it may have changed.
- initializationuses · The process of establishing initial values or a starting state for a program entity, component, resource, or computation before subsequent operation.
- release operationimplements · An atomic operation with release ordering that synchronizes with a corresponding acquire operation, making preceding effects visible.
Questions about call_once
What is 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.
Which Manning books explain call_once?
Modern C, Third Edition, C++ Concurrency in Action, Second Edition and Modern C.
What should I know before learning call_once?
Manning books that teach call_once usually introduce command-line argument and static storage first.
What is the difference between call_once and atexit?
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. atexit is a C standard-library function that registers a function to execute during normal program termination.
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.


