4 ms·
I don't understand how the double-checked locking recommendation is safe. Someone asked about it in the comments, but the response didn't make sense to me. It s
by clockworkweeble 12y ago
I don't understand how the double-checked locking recommendation is safe. Someone asked about it in the comments, but the response didn't make sense to me. It seems that the call to IsLinked would introduce a data-race with adjacent deletions, and cause undefined behaviour, unless there is already locking at an outer scope. If there is such locking, the lock inside the destructor is redundant. Have I misunderstood?
It has generally been my experience that if control enters a destructor while other threads are concurrently accessing the object, you are in a world of pain. For one thing, this makes it near impossible to safely subclass the object. (Because the subclass destructor will run first, rendering its invariants invalid before the base class has done whatever it needs to do to unlink the object from the outside world.) Anecdotally, I've also found code like this to be deadlock-prone, but I couldn't say for sure if the two are causally linked.