proomt

Search

Search posts, papers, and topics

All posts

Raymond Chen3 min readintermediate

Magic statics vs. std::call_once

Summary

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…

  • Magic statics are concise and thread‑safe, but only work for static objects and are shared across all calls.
  • `std::call_once` lets you lazily initialize non‑static members per instance, avoiding the cross‑instance sharing bug.
  • Encapsulating `std::call_once` in a `lazy<T>` type (using `std::optional` and a once_flag) provides a reusable on‑demand initializer.
  • Singletons are often implemented with a function‑local static; the same pattern can be used for any one‑time init that is truly static.

Choosing the right lazy‑initialization primitive prevents subtle bugs where per‑object state is inadvertently shared, and it keeps initialization cost out of the hot path while remaining thread‑safe.

6/10

Related reading

  1. 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. Kodebits Day 95: Lazy Sequences [FREE]

    This short Kotlin challenge demonstrates lazy sequences. It shows how `asSequence()` combined with `map`, `filter`, and `take` processes elements one at a time, evaluating lazily only when a terminal operation is called.

    Ray Wenderlichkodeco.com1 min
  3. App Hardening: One Obfuscation Pipeline Across Every Port

    Codename One adds a cloud‑side hardening step that runs on the merged JAR before it is split into Android, iOS, JavaScript, and desktop binaries. It can rename symbols, encrypt string literals, and insert opaque‑predicate control‑flow guards at configurable levels (off → standard → aggressive → paranoid). The transforms are selective per platform to avoid breaking optimizers, and a mapping is kep…

    CodeName Onecodenameone.com6 min
  4. Secure Compute and Static IP builds start 64% faster

    Vercel’s Secure Compute and Static IP builds now start 64% faster by using prewarmed containers, cutting the time from deployment creation to build start from 6.7 s to 2.4 s. The change is automatic and requires no user action.

    Vercelvercel.com1 minrelease
  5. Textbook review: Is Parallel Programming Hard, And, If So, What Can You Do About It?

    A detailed, personal review of Paul McKenney’s free online textbook on parallel programming. The author, coming from a TLA⁺/distributed‑systems background, finds the early chapters excellent for building intuition about CPU caches, memory ordering, and false‑sharing, but notes gaps (e.g., shallow coverage of C++11 atomics and MESI). The review is concrete, cites specific chapters, and offers prac…

    Lobstersahelwer.ca8 minlobste.rs21