4 ms·
No, you don't want two threads trying to allocate the object at once. You still need a locked init.
by jamesaguilar 14y ago
No, you don't want two threads trying to allocate the object at once. You still need a locked init.
- vy8vWJlco 14y agoYes, there would be an allocation race, so if you cannot accept allocating unnecessarily in a discardable/recoverable way (but also one that preserves the semantics of the singleton: one and only one instance returned over its life), you would still need a flag. Though you might instead use an "incomplete"/place-holder reference value to simulate the flag and prevent duplication of effort/wasting resources while the allocation happens - all in the space of the pointer.