{"id":6442,"date":"2022-09-19T07:36:33","date_gmt":"2022-09-19T07:36:33","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/creational-patterns-singleton\/"},"modified":"2023-06-26T08:59:52","modified_gmt":"2023-06-26T08:59:52","slug":"creational-patterns-singleton","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/creational-patterns-singleton\/","title":{"rendered":"The Singleton"},"content":{"rendered":"<p>The most controversial Design Pattern from the book&nbsp; &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_Patterns\">Design Patterns: Elements of Reusable Object-Oriented Software&#8221;<\/a> is the Singleton Pattern. Let me introduce it before I discuss its pros and cons.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6435\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/CreationalPatterns.png\" alt=\"CreationalPatterns\" width=\"650\" height=\"330\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/CreationalPatterns.png 1220w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/CreationalPatterns-300x152.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/CreationalPatterns-1024x520.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/CreationalPatterns-768x390.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/>&nbsp;<\/p>\n<p>The Singleton Pattern is a creational pattern. Here are the facts:<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Singleton_Pattern\"><\/span>Singleton Pattern<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Purpose\"><\/span>Purpose<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Guarantees that only one instance of a class exists<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Use_Case\"><\/span>Use Case<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>You need access to some shared resource<\/li>\n<li>This shared resource must exist only once<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Example\"><\/span>Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Boost serialization defines a template &#8220;<em>which will convert any class into a singleton with the following features<\/em>&#8220;: <a href=\"https:\/\/www.boost.org\/doc\/libs\/1_80_0\/boost\/serialization\/singleton.hpp\">https:\/\/www.boost.org\/doc\/libs\/1_80_0\/boost\/serialization\/singleton.hpp<\/a><\/li>\n<li>It performs its job by deriving from the class <a href=\"https:\/\/www.boost.org\/doc\/libs\/1_80_0\/libs\/core\/doc\/html\/core\/noncopyable.html\">boost::noncopyable<\/a>:<\/li>\n<\/ul>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">BOOST_SYMBOL_VISIBLE<\/span> singleton_module <span style=\"color: #555555;\">:<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">public<\/span> boost<span style=\"color: #555555;\">::<\/span>noncopyable\r\n{\r\n  ...\r\n};\r\n<\/pre>\n<\/div>\n<h3><span class=\"ez-toc-section\" id=\"Structure\"><\/span>Structure<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6440\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/SingletonStructure.png\" alt=\"SingletonStructure\" width=\"350\" height=\"226\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/SingletonStructure.png 401w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/SingletonStructure-300x194.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong><code>instance<\/code> (static)<\/strong><\/p>\n<ul>\n<li>A private instance of Singleton<\/li>\n<\/ul>\n<p><code><strong>getInstance (static)<\/strong><\/code><\/p>\n<ul>\n<li>A public member function that returns an instance<\/li>\n<li>Creates the instance<\/li>\n<\/ul>\n<p><strong><code>Singleton<\/code><\/strong><\/p>\n<ul>\n<li>Private constructor<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Calling the member function<code> getInstance<\/code> is the only way to create a Singleton. Additionally, the Singleton must not support copy semantics.<\/p>\n<p>This brings me to its implementation in modern C++.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Related_Patterns\"><\/span>Related Patterns<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>The creational patterns <a href=\"https:\/\/en.wikipedia.org\/wiki\/Abstract_factory_pattern\">Abstract Factory<\/a>, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Builder_pattern\">Builder<\/a>, and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Prototype_pattern\">Prototype <\/a>are often singletons.<\/li>\n<li>The structural pattern <a href=\"https:\/\/en.wikipedia.org\/wiki\/Facade_pattern\">Facade <\/a>is often a singleton.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Implementation\"><\/span>Implementation<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In the following lines, I discuss various implementation variants of the Singleton. Let me start with the classical implementation of the Singleton Pattern.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Classical_Implementation\"><\/span>Classical Implementation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Here is the implementation, based on the &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_Patterns\">Design Patterns: Elements of Reusable Object-Oriented Software&#8221;<\/a> (Design Patterns) book:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ singleton.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MySingleton<\/span>{\r\n\r\n  <span style=\"color: #9999ff;\">private:<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> MySingleton<span style=\"color: #555555;\">*<\/span> instance;                            <span style=\"color: #0099ff;\"><em>\/\/ (1)<\/em><\/span>\r\n    MySingleton() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\r\n    <span style=\"color: #555555;\">~<\/span>MySingleton() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\r\n\r\n  <span style=\"color: #9999ff;\">public:<\/span>\r\n    MySingleton(<span style=\"color: #006699; font-weight: bold;\">const<\/span> MySingleton<span style=\"color: #555555;\">&amp;<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">delete<\/span>;\r\n    MySingleton<span style=\"color: #555555;\">&amp;<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">=<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> MySingleton<span style=\"color: #555555;\">&amp;<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">delete<\/span>;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> MySingleton<span style=\"color: #555555;\">*<\/span> <span style=\"color: #cc00ff;\">getInstance<\/span>(){\r\n      <span style=\"color: #006699; font-weight: bold;\">if<\/span> ( <span style=\"color: #555555;\">!<\/span>instance ){\r\n        instance <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> MySingleton();\r\n      }\r\n      <span style=\"color: #006699; font-weight: bold;\">return<\/span> instance;\r\n    }\r\n};\r\n\r\nMySingleton<span style=\"color: #555555;\">*<\/span> MySingleton<span style=\"color: #555555;\">::<\/span>instance <span style=\"color: #555555;\">=<\/span> nullptr;               <span style=\"color: #0099ff;\"><em> \/\/ (2)<\/em><\/span>\r\n\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"MySingleton::getInstance(): \"<\/span><span style=\"color: #555555;\">&lt;&lt;<\/span> MySingleton<span style=\"color: #555555;\">::<\/span>getInstance() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"MySingleton::getInstance(): \"<\/span><span style=\"color: #555555;\">&lt;&lt;<\/span> MySingleton<span style=\"color: #555555;\">::<\/span>getInstance() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The original implementation used a protected default constructor. Additionally, I explicitly deleted copy semantics (copy constructor and copy assignment operator). I will write more about copy semantics and move semantics later.<\/p>\n<p>The output of the program shows that there is only one instance of the class <code>MySingleton<\/code>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6441\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/singleton.png\" alt=\"singleton\" width=\"419\" height=\"231\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/singleton.png 419w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/singleton-300x165.png 300w\" sizes=\"auto, (max-width: 419px) 100vw, 419px\" \/><\/p>\n<p>This implementation of the Singleton requires C++11. With C++17, the declaration (line 2) and definition (line 2) of the static instance variable <code>instance<\/code> can be directly done in the class:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MySingleton<\/span>{\r\n\r\n  <span style=\"color: #9999ff;\">private:<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">inline<\/span> <span style=\"color: #006699; font-weight: bold;\">static<\/span> MySingleton<span style=\"color: #555555;\">*<\/span> instance{nullptr};  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    MySingleton() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\r\n    <span style=\"color: #555555;\">~<\/span>MySingleton() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\r\n\r\n  <span style=\"color: #9999ff;\">public:<\/span>\r\n    MySingleton(<span style=\"color: #006699; font-weight: bold;\">const<\/span> MySingleton<span style=\"color: #555555;\">&amp;<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">delete<\/span>;\r\n    MySingleton<span style=\"color: #555555;\">&amp;<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">=<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> MySingleton<span style=\"color: #555555;\">&amp;<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">delete<\/span>;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> MySingleton<span style=\"color: #555555;\">*<\/span> <span style=\"color: #cc00ff;\">getInstance<\/span>(){\r\n      <span style=\"color: #006699; font-weight: bold;\">if<\/span> ( <span style=\"color: #555555;\">!<\/span>instance ){\r\n        instance <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> MySingleton();\r\n      }\r\n      <span style=\"color: #006699; font-weight: bold;\">return<\/span> instance;\r\n    }\r\n};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Line 3 performs the declaration and definition in one step.<\/p>\n<p>What are the weaknesses of this implementation? Let me name the static initialization order fiasco and concurrency.<\/p>\n<h4>Static Initialization Order Fiasco<\/h4>\n<p>Static variables in one translation unit are initialized according to their definition order. In contrast, the initialization of static variables between translation units has a severe issue. When one static variable <code>staticA<\/code> is defined in one translation unit and another static variable<code> staticB<\/code> is defined in another translation unit, and <code>staticB<\/code> needs<code> staticA<\/code> to initialize itself, you end up with the static initialization order fiasco. The program is ill-formed because you have no guarantee which static variable is initialized first at run time.<\/p>\n<p>For completeness: Static variables are initialized according to their definition order and destructed in the reverse order. Accordingly, no ordering guarantees are provided for initialization or destruction between translation units. The translation unit is what to get after the preprocessor run.&nbsp;<\/p>\n<p>What does this have to do with Singletons? Singletons are statics in disguise. When, therefore, one Singleton&#8217;s initialization depends on another Singleton&#8217;s initialization in another translation unit, you could end in the static initialization order fiasco.<br \/>Before I write about the solution, let me show you the static initialization order fiasco in action.<\/p>\n<ul>\n<li>\n<p><strong>A 50:50 Chance to get it Right<\/strong><\/p>\n<\/li>\n<\/ul>\n<p>What is unique about the initialization of statics? The initialization order of statics happens in two steps: at compile time and at run time.<\/p>\n<p>When a static cannot be const-initialized during compile time, it is zero-initialized. At run time, the dynamic initialization happens for these statics that were zero-initialized.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ sourceSIOF1.cpp<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">square<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> n) {\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> n <span style=\"color: #555555;\">*<\/span> n;\r\n}\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> staticA  <span style=\"color: #555555;\">=<\/span> square(<span style=\"color: #ff6600;\">5<\/span>); \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ mainSOIF1.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">extern<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> staticA;                  <em><span style=\"color: #0099ff;\">\/\/ (1)<\/span><\/em>\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> staticB <span style=\"color: #555555;\">=<\/span> staticA;     \r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"staticB: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> staticB <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Line (1) declares the static variable <code>staticA<\/code>. The following initialization of <code>staticB<\/code> depends on the initialization of<code> staticA<\/code>. But <code>staticB<\/code> is zero-initialized at compile time and dynamically initialized at run time. The issue is that there is no guarantee in which order <code>staticA<\/code> or <code>staticB<\/code> are initialized because <code>staticA<\/code> and <code>staticB<\/code> belong to different translation units. You have a 50:50 chance that <code>staticB<\/code> is 0 or 25.<\/p>\n<p>To demonstrate this problem, I can change the link order of the object files. This also changes the value for <code>staticB<\/code>!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5941\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/StaticInitializationOrderFiasco.png\" alt=\"StaticInitializationOrderFiasco\" width=\"600\" height=\"338\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/StaticInitializationOrderFiasco.png 686w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/StaticInitializationOrderFiasco-300x169.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>What a fiasco! The result of the executable depends on the link order of the object files. What can we do?<\/p>\n<h3><span class=\"ez-toc-section\" id=\"The_Meyers_Singleton\"><\/span>The Meyers Singleton<strong><br \/><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Static variables with local scope are created when they are used the first time. Local scope essentially means that the <code>static<\/code> variable is surrounded in some way by curly braces. This lazy initialization is a guarantee that C++98 provides. The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Scott_Meyers\">Meyers <\/a>Singleton is exactly based on this idea. Instead of a <code>static<\/code> instance of type Singleton, it has a local static of type Singleton.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ singletonMeyer.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MeyersSingleton<\/span>{\r\n\r\n  <span style=\"color: #9999ff;\">private:<\/span>\r\n\r\n    MeyersSingleton() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\r\n    <span style=\"color: #555555;\">~<\/span>MeyersSingleton() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\r\n\r\n  <span style=\"color: #9999ff;\">public:<\/span>\r\n\r\n    MeyersSingleton(<span style=\"color: #006699; font-weight: bold;\">const<\/span> MeyersSingleton<span style=\"color: #555555;\">&amp;<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">delete<\/span>;\r\n    MeyersSingleton<span style=\"color: #555555;\">&amp;<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span> <span style=\"color: #555555;\">=<\/span> (<span style=\"color: #006699; font-weight: bold;\">const<\/span> MeyersSingleton<span style=\"color: #555555;\">&amp;<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">delete<\/span>;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> MeyersSingleton<span style=\"color: #555555;\">&amp;<\/span> getInstance(){\r\n      <span style=\"color: #006699; font-weight: bold;\">static<\/span> MeyersSingleton instance;        <em><span style=\"color: #0099ff;\">\/\/ (1)<\/span><\/em>\r\n      <span style=\"color: #006699; font-weight: bold;\">return<\/span> instance;\r\n    }\r\n};\r\n\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"&amp;MeyersSingleton::getInstance(): \"<\/span><span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #555555;\">&amp;<\/span>MeyersSingleton<span style=\"color: #555555;\">::<\/span>getInstance() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"&amp;MeyersSingleton::getInstance(): \"<\/span><span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #555555;\">&amp;<\/span>MeyersSingleton<span style=\"color: #555555;\">::<\/span>getInstance() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><code>static MeyersSingleton instance <\/code>in line (1) is a <code>static<\/code> with local scope. Consequentially, it lazy initialized and can not be a victim of the static initialization order fiasco. With C++11, the Meyers Singleton become even more powerful.<\/p>\n<h4>Concurrency<\/h4>\n<p>With C++11, static variables with local scope are also initialized in a thread-safe way. This means that the Meyers Singleton does not only solve the static initialization order fiasco, but also guarantees that the Singleton is initialized in a thread-safe way. Additionally, using the locals static for the thread-safe initialization of a Singleton is the easiest and fastest way. I have already written two posts about the thread-safe initialization of the Singelton.<\/p>\n<ul>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/thread-safe-initialization-of-data\">Thread-Safe Initialization of Data<\/a><\/li>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/thread-safe-initialization-of-a-singleton\">Thread-Safe Initialization of a Singleton<\/a><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Whats_Next\"><\/span>What&#8217;s Next?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Singleton Pattern is highly emotional. My post &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/thread-safe-initialization-of-a-singleton\">Thread-Safe Initialization of a Singleton<\/a>&#8221; was so far read by more than 300&#8217;000 people. Let me, therefore, discuss in my next posts the pros and cons of the Singleton and possible alternatives.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The most controversial Design Pattern from the book&nbsp; &#8220;Design Patterns: Elements of Reusable Object-Oriented Software&#8221; is the Singleton Pattern. Let me introduce it before I discuss its pros and cons.<\/p>\n","protected":false},"author":21,"featured_media":6435,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[404],"class_list":["post-6442","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns","tag-singleton"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6442"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6442\/revisions"}],"predecessor-version":[{"id":6659,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6442\/revisions\/6659"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6435"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}