proomt

Search

Search posts, papers, and topics

cpp

RSS
  1. 2

    std::call_once vs. std::async

    Raymond Chen compares using std::call_once versus std::async (deferred) for lazy initialization in C++. He shows that std::async can be wrapped in a shared_future for repeatable get() calls, but it adds heap allocation and extra code, while call_once is tiny; exception behavior also differs.

    Raymond Chenmicrosoft.com2 min
  2. 3

    Magic statics vs. std::call_once

    Function‑local "magic" statics give you thread‑safe, one‑time init for static data, but they’re limited to static storage and shared across all instances. Use `std::call_once` (or a small `lazy<T>` wrapper) when you need per‑object lazy init or when the value isn’t a static. The post shows concrete code for both approaches, explains the pitfalls of using a static inside a member function, and ske…

    Raymond Chenmicrosoft.com3 min