Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I have read many questions considering thread-safe double checked locking (for singletons or lazy init). In some threads, the answer is that the pattern is entirely broken, others suggest a solution.

So my question is: Is there a way to write a fully thread-safe double checked locking pattern in C++? If so, how does it look like.

We can assume C++11, if that makes things easier. As far as I know, C++11 improved the memory model which could yield the needed improvements.

I do know that it is possible in Java by making the double-check guarded variable volatile. Since C++11 borrowed large parts of the memory model from the one of Java, so I think it could be possible, but how?

share|improve this question
4  
If you can use C++11 just ignore the whole double checked locking business and use either static local variables or std::call_once. – R. Martinho Fernandes Sep 6 '12 at 14:12
    
Are static locals initialized lazily? And about call_once: How does this ensure that call once will not write the not fully created reference to the variable? – gexicide Sep 6 '12 at 14:14
3  
yes, static locals are initialized lazily in a thread-safe manner. And call_once ensures the subject is only ever called once; and that no other call to call_once returns before the one that actually executes the function returns (you can read more here en.cppreference.com/w/cpp/thread/call_once). How it does that is up to the implementation. These two things basically exist so you don't want to bother with writing more <strike>bugs</strike> double-checked locking implementations. – R. Martinho Fernandes Sep 6 '12 at 14:16
    
Static local variables must surely be one of the most elegant solutions. – Kerrek SB Sep 6 '12 at 14:19
    
still, call_once might not yield the performance benefits of the double-checked-locking pattern. It only ensures one time execution. However, the static local var thingy could be the solution. – gexicide Sep 6 '12 at 14:19
up vote 15 down vote accepted

Simply use a static local variable for lazily initialized Singletons, like so:

MySingleton* GetInstance() {
  static MySingleton instance;
  return &instance; 
}

The (C++11) standard already guarantees that static variables are initialized in a threadsafe manner and it seems likely that the implementation of this at least as robust and performant as anything you'd write yourself.

The threadsafety of the initialization can be found in §6.7.4 of the (C++11) standard:

If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.

share|improve this answer
10  
This is insane. Why, oh why, would you ever return a pointer if it can never be null ? – Matthieu M. Sep 6 '12 at 15:21
1  
@MatthieuM.: Mainly because it makes people less inclined to copy the underlying object. Of course a well designed singleton shouldn't have a copy constructor, but still. I really don't see how return by reference vs. return by value matters in that case. Therefore I would hardly call that insane. – Grizzly Sep 6 '12 at 23:14
2  
@Grizzly: If an object should not be copyable, it is up to the object to enforce that. If an object should have an instance that is globally accessible, there should be a function to handle that (like yours). These two things are separate, and there's no reason to combine them. This is why the Singleton pattern is stupid. – GManNickG Sep 7 '12 at 3:00
1  
Insane because some pople might delete MySingleton::GetInstance, or might if ( MySingleton::GetInstance() ) {}. It makes more sense to return a reference and set copy ctor/assignment to deleted. Or if it has to be a pointer make the dtor private. – paulm Apr 25 '14 at 22:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.