Skip to content

Singleton

要求

为了确保只有一个object,需要满足如下要求:

1、只能够通过class static method getInstance()获得对象,其它方式都不可,包括:

a、C c

因此,需要将constructor私有化。

2、不能够copy

Meyer's singleton

Meyer's singleton就是利用static local object的特性来实现的,参见Static-local-object章节。

stackoverflow C++ Singleton design pattern

A

In 2008 I provided a C++98 implementation of the Singleton design pattern that is lazy-evaluated, guaranteed-destruction, not-technically-thread-safe: Can any one provide me a sample of Singleton in c++?

Here is an updated C++11 implementation of the Singleton design pattern that is lazy-evaluated, correctly-destroyed, and thread-safe.

class S
{
    public:
        static S& getInstance()
        {
            static S    instance; // Guaranteed to be destroyed.
                                  // Instantiated on first use.
            return instance;
        }
    private:
        S() {}                    // Constructor? (the {} brackets) are needed here.

        // C++ 03
        // ========
        // Don't forget to declare these two. You want to make sure they
        // are inaccessible(especially from outside), otherwise, you may accidentally get copies of
        // your singleton appearing.
        S(S const&);              // Don't Implement
        void operator=(S const&); // Don't implement

        // C++ 11
        // =======
        // We can use the better technique of deleting the methods
        // we don't want.
    public:
        S(S const&)               = delete;
        void operator=(S const&)  = delete;

        // Note: Scott Meyers mentions in his Effective Modern
        //       C++ book, that deleted functions should generally
        //       be public as it results in better error messages
        //       due to the compilers behavior to check accessibility
        //       before deleted status
};

See this article about when to use a singleton: (not often) Singleton: How should it be used

See this two article about initialization order and how to cope: Static variables initialisation order Finding C++ static initialization order problems

See this article describing lifetimes: What is the lifetime of a static variable in a C++ function?

See this article that discusses some threading implications to singletons: Singleton instance declared as static variable of GetInstance method, is it thread-safe?

See this article that explains why double checked locking will not work on C++: What are all the common undefined behaviours that a C++ programmer should know about? Dr Dobbs: C++ and The Perils of Double-Checked Locking: Part I

Thread safe

C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming? # A 的如下comment中,对这个问题进行了讨论:

In most cases there is no sence to implement the double-checked locking initialization in C++11 since local static variable initialization is already thread-safe. – newbie Jun 17 '15 at 21:29

@newbie: True, C+11 codified common practice and made the "Meyers singleton" thread-safe. I was just providing an example of the sort of thing you can do with the low-level synchronization primitives. – Nemo Jun 17 '15 at 22:34