{"id":82,"date":"2009-02-12T13:24:30","date_gmt":"2009-02-12T11:24:30","guid":{"rendered":"http:\/\/dev.flauschig.ch\/wordpress\/?p=82"},"modified":"2009-08-21T09:00:20","modified_gmt":"2009-08-21T07:00:20","slug":"thread-safe-singleton-pattern","status":"publish","type":"post","link":"http:\/\/dev.flauschig.ch\/wordpress\/?p=82","title":{"rendered":"Thread Safe Singleton Pattern"},"content":{"rendered":"<p>Singletons can be great. But it&#8217;s important to make sure that the Singleton really is a Singleton, even in a Multi-Threading Environment.<\/p>\n<p>There are multiple possibilities to achieve this and here are my Favorites:<!--more--><\/p>\n<p>Nice and clean for almost all cases:<\/p>\n<pre class=\"brush: csharp; gutter: false; title: ; toolbar: true; notranslate\" title=\"\">\r\npublic sealed class Singleton\r\n{\r\n    static readonly Singleton _instance = new Singleton();\r\n\r\n    \/\/ Don't mark the Class as beforefieldinit\r\n    static Singleton() { }\r\n\r\n    private Singleton() { }\r\n\r\n    public static Singleton Instance\r\n    {\r\n        get\r\n        {\r\n            return _instance;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Full lazy Implementation:<\/p>\n<pre class=\"brush: csharp; gutter: false; title: ; toolbar: true; notranslate\" title=\"\">\r\npublic sealed class Singleton\r\n{\r\n    private Singleton()  { }\r\n\r\n    public static Singleton Instance\r\n    {\r\n        get\r\n        {\r\n            return Nested.instance;\r\n        }\r\n    }\r\n\r\n    private class Nested\r\n    {\r\n        \/\/ Don't mark the Class as beforefieldinit\r\n        static Nested() { }\r\n\r\n        internal static readonly Singleton _instance = new Singleton();\r\n    }\r\n}\r\n<\/pre>\n<p>Many may use this implementation but it has several drawbacks and may not always work as expected (for example doesn&#8217;t work with JAVA). So just don&#8217;t use it.<br \/>\n<span style=\"color: #ff0000;\">BAD CODE:<\/span><\/p>\n<pre class=\"brush: csharp; gutter: false; title: ; toolbar: true; notranslate\" title=\"\">\r\npublic sealed class Singleton\r\n{\r\n    static Singleton _instance = null;\r\n    static readonly object padlock = new object();\r\n\r\n    private Singleton() { }\r\n\r\n    public static Singleton Instance\r\n    {\r\n        get\r\n        {\r\n            if (_instance == null)\r\n            {\r\n                lock (padlock)\r\n                {\r\n                    if (_instance==null)\r\n                    {\r\n                        _instance = new Singleton();\r\n                    }\r\n                }\r\n            }\r\n            return _instance;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Singletons can be great. But it&#8217;s important to make sure that the Singleton really is a Singleton, even in a Multi-Threading Environment. There are multiple possibilities to achieve this and here are my Favorites:<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4,3],"tags":[40,39,37,70,69,38,68],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-roemer","4":"has-more-link","5":"post-82","7":"format-standard","8":"category-csharp","9":"category-programming","10":"post_tag-double-locking","11":"post_tag-lazy","12":"post_tag-multi-threading","13":"post_tag-pattern","14":"post_tag-singleton","15":"post_tag-static","16":"post_tag-thread-save"},"acf":[],"views":5800,"_links":{"self":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/82"}],"collection":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=82"}],"version-history":[{"count":0,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/82\/revisions"}],"wp:attachment":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}