3 ms·
Go offers this out of the box via the sync.Once function. Do other languages? Kind of surprised python doesn’t as this sort of pattern is common in applications
by waterside81 6y ago
Go offers this out of the box via the sync.Once function. Do other languages? Kind of surprised python doesn’t as this sort of pattern is common in applications dealing with concurrency
- ninkendo 6y agoApple’s Grand Central Dispatch concurrency library has dispatch_once [0], which does something similar. It relies on non-standard “block” extensions to C, which are a way of defining lambda functions, and in practice you only see it used in Apple platforms. [0] https://developer.apple.com/documentation/dispatch/1447169-dispatch_once https://developer.apple.com/documentation/dispatch/1447169-d...
- fishywang 6y agolazy init in kotlin and scala is essentially the same thing. the good thing with go's sync.Once is that it's implemented as a library instead of something in the language itself, so it's easy for curious user to see how it's actually implemented. they even have comments there pointing out wrong implementations, which I have seen people make the exact same mistake during code reviews (in other language).
- dnautics 6y agoErlang has features for this baked in. What's more, if initialization of any subcomponent fails (say one of its dependencies hadn't completed booting yet due to race condition), if the author made it throw, the dependent subcomponent will automatically restart itself and try again. There are also one line strategies for trying again later, etc, so you don't even have to worry about blocking to prevent those race conditions. > Kind of surprised python doesn’t as this sort of pattern is common in applications dealing with concurrency Well yeah, python was not designed for that.