3 ms·
It's well-known that you can do double-checked locking right with memory barriers, but memory barriers aren't always cheaper. On TSO architectures (x86, most SP
by rbehrends 13y ago
It's well-known that you can do double-checked locking right with memory barriers, but memory barriers aren't always cheaper. On TSO architectures (x86, most SPARCs), read barriers are no-ops, but with weaker memory models (e.g., ARM), they aren't.
Edit: Also, the memory barriers are in the wrong place in the code. You need a read barrier between the init check and returning ptr, and a write barrier between the actual initialization of the object and writing the init variable.
- jamesaguilar 13y agoInteresting. I didn't know that. Sorry for the error, I'm not an expert in this subtle stuff. Barely even conversant to be honest. On those architectures with weaker models the real simpler thing to do is to make the singleton thread-unsafe for initialization.