logoalt Hacker News

TuxSHtoday at 1:29 AM1 replyview on HN

> But today you can just use std::sync::LazyLock which lets you say OK, the first time somebody wants this thing we'll make it, and subsequently it just exists forever.

This corresponds to a C++ feature that has been added in C++11 (and other languages have this too AFAIK); good for libraries but but this has a runtime performance cost.

Since C++20 you can use constinit, or std::construct_at to avoid static initialization woes entirely

> Intrusive lists are one of those things where technically you might need them, but so often they're just because a C or C++ programmer knew how to write one.

True about C, and also true of most userspace applications.

However, the whole point of intrusive list (and other intrusive structures) is to flip the ownership relationship - the object owns the node, instead of the node owning the object. In other words, the intrusive list is a non-owning view over its elements. More importantly, they allow the same object to be present in multiple lists at once (as long as you add enough node hooks).

For example, you can have a pool-allocated Thread object that is simultaneously part of a queue of inactive threads of priority N, and also part of a wait queue for something else.

> A retained mode GUI looks like a hierarchy to me, how do you say it isn't?

Right, I conflated it with the other examples, my bad; there it's because of shared mutable state, children elements needing to update their parents, and lack of inheritance in Rust


Replies

tialaramextoday at 8:31 AM

> Since C++20 you can use constinit, or std::construct_at to avoid static initialization woes entirely

This is just yet another bad defaults situation. Yes, that's the correct behaviour and so it's what Rust's statics do, but it wasn't how C++ worked so they had to add this constinit keyword. LazyStatic is solving the other thing - where we don't need runtime parameters but we are doing work at runtime so we aren't truly constant.