Lazy initialization with singleton pattern

后端 未结 2 1665
無奈伤痛
無奈伤痛 2021-01-04 06:32

Would the following code facilitate lazy initialization?
Or would the singletonInstance be created as soon as somebody includes the header

相关标签:
2条回答
  • 2021-01-04 07:04

    This is known as the Meyers singleton and they are lazy instantiated.

    There are some considerations:

    • the singletons will be destroyed at the end of the program in the reverse order in which they are created, so there can be dependency issues.
    • C++03 doesn't guarantee against race conditions in multithreaded environments.
    0 讨论(0)
  • 2021-01-04 07:18

    The SingletonClass constructor will not be called earlier than somenone calls the Instance() method.

    Thus yes, it facilitates lazy initialization.

    0 讨论(0)
提交回复
热议问题