0

I am aware that C++11 insure that a static variable inside a block scope is thread safe and is always created once only. Using this mysingelton looks something like below.

class singleton
{
    singleton(){}
    singleton(const singleton& s)=delete;
    singleton& operator=(const singleton& s)=delete;

    singleton(singleton&& s)=delete;
    singleton& operator=( singleton&& s)=delete;
    
    public:
     static singleton* GetInstance();
     static std::shared_ptr<singleton> GetInstanceSP();
          
};

singleton* singleton::GetInstance()
{
        static singleton sObject;
        return &sObject;
}

But this will have an issue, sObject will obviously be destructed before the Object which is using it if the caller is static or global. So I tried below solution which looks and working good

std::shared_ptr<singleton> singleton::GetInstanceSP()
{
        static std::shared_ptr<singleton> sp(new singleton);
        return sp;
}

Is this solution OK? or still it has some loopholes. I didn't go for unique_ptr for same reason as its destructor will be called before its client.

Also one more query: Will below code is also thread safe? if so why this is not in any book or refrence example

static singelton *sp(new singelton);
reurn sp;

W

  • 1
    "But this will have an issue, sObject will obviously be destructed before the Object which is using it." I'm not quite sure how does the conclusion come from. Is the caller object which is using it is another static object as well? – Louis Go Jul 11 '23 at 06:11
  • In case caller is another static or global. In both case it would have been created before singleton object and thus will be destructed after sObject. – Rakesh Mehta Jul 11 '23 at 06:14
  • You may refer to [storage duration](https://en.cppreference.com/w/cpp/language/storage_duration) -> static storage duration. In short, static objects are constructed when program starts and destructed when program ends. It should be suffice in most of cases, unless you have singletons use other singletons – Louis Go Jul 11 '23 at 06:14
  • 1
    That's *a lot* different from your current question. Please specify that the caller is also a static object as well by [edit]. – Louis Go Jul 11 '23 at 06:16
  • I am aware of varaibles lifetime concepts. I forgot to mention the case that caller would be static/global. I'm looking for answers of other questions. – Rakesh Mehta Jul 11 '23 at 06:17
  • 2
    In any case, construct and destruct time manually is the easiest way. But you may wait for a comprehensive answer. – Louis Go Jul 11 '23 at 06:19
  • https://stackoverflow.com/a/335746/4123703 might solve your problem but my gut feeling is that's just pushing the problem to future maintainer( most of time it's *you*) . My sincere answer is don't make static object destruction depends on anyone. – Louis Go Jul 11 '23 at 06:28
  • kind of related: https://stackoverflow.com/questions/29822181/prevent-static-initialization-order-fiasco-c (should be "deinitialization order fiasco", but the solutions are the same) – YSC Jul 11 '23 at 11:47
  • Re: "static variable inside a block scope is thread safe" -- this is not correct. It's **construction** is thread-safe; subsequent use isn't protected in any way. – Pete Becker Jul 11 '23 at 12:10
  • Your second solution is no better than the first: using your class in destructors of statics/globals can lead to the same using-the-destroyed-object problem as the first. The third solution would work, and the reason it is not that frequent is the pseudo-leaks that it invites. Which can be real leaks if the singleton manages a resource that is not automatically released by language or OS. – Mike Tyukanov Jul 12 '23 at 09:54

0 Answers0