{"id":1164,"date":"2010-08-17T09:31:57","date_gmt":"2010-08-17T08:31:57","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=1164"},"modified":"2010-08-17T09:31:57","modified_gmt":"2010-08-17T08:31:57","slug":"threads-singleton","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/08\/17\/threads-singleton\/","title":{"rendered":"threads &#8211; singleton"},"content":{"rendered":"<p>As from one of my previous posts about <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/05\/07\/threading-running-different-parts-of-the-program-at-once\/\">threading in csharp<\/a> (c#), well with using <a href=\"http:\/\/en.wikipedia.org\/wiki\/Singleton_pattern\">Singletons<\/a> you can use similar data between two different threads running.<\/p>\n<p>In basics a singleton is when you want to have a single class instance so that you can share this class so that every time that you use it (even across threads) will only access the same class, it is very useful for when you have a printer spool so that you do not want to have x amount of printer spools (spool is when you have a list of print tasks waiting to print) and thus you only want to have one instance of a printer spool !!. <\/p>\n<p>I have used the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff650316.aspx\">singleton creation from Microsoft<\/a> website, that creates a singleton class that is thread safe which means that I am using the lock method that will lock on a object to stop thread contention and thus only creates a new instance of a Singleton class so that each thread will only get access to a single instance of that class.<\/p>\n<p>So when you want to gain access to that single instance, you just call the<\/p>\n<pre lang=\"csharp\">\r\nSingleton theSingleton = Singleton.Instance;<\/pre>\n<p>So here is the full source code, and below is the output where the output is displaying the value is incrementing otherwise if is was not a singleton class, the main class would print out 0-4 and also the runthismethod would output 0-9 instead!.<\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\nusing System.Threading;\r\n \r\nnamespace monotestproject\r\n{\r\n\tpublic sealed class Singleton\r\n\t{\r\n\t\tprivate static int _value;\r\n\t\tprivate static volatile Singleton instance;\r\n\t\tprivate static object syncRoot = new Object();\r\n\t\t\r\n\t\tpublic Singleton() { _value = 0;}\r\n\r\n\t\tpublic static Singleton Instance\r\n\t\t{\r\n\t\t\tget { \r\n\t\t\t\tif (instance == null)\r\n\t\t\t\t{\r\n\t\t\t\t\tlock(syncRoot)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tif (instance == null)\r\n\t\t\t\t\t\t\tinstance = new Singleton();\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\treturn instance;\r\n\t\t\t}\r\n\t\t\tprivate set {}\r\n\t\t}\r\n\t\t\r\n\t\tpublic int theValue \r\n\t\t{\r\n\t\t\tget { return _value;}\r\n\t\t\tset { _value = value;}\r\n\t\t}\r\n\t}\r\n\t\r\n\tclass MainClass\r\n\t{\r\n\t\tpublic static void Main (string[] args)\r\n\t\t{\r\n\t\t\tSingleton theSingleton = Singleton.Instance;\r\n\t\t\t\/\/ initialize the RunThisMethod as a thread\r\n\t\t\tThread theThread = new Thread(RunThisMethod);\r\n\t\t\ttheThread.Start();\r\n \t\t\t\r\n\t\t\tfor (int j = 0; j < 5; j++)\r\n\t\t\t{\r\n\t\t\t\ttheSingleton.theValue++;\r\n\t\t\t\tConsole.WriteLine(\"Main Singleton value \" + theSingleton.theValue);\r\n\t\t\t\tThread.Sleep(100);\r\n\t\t\t}\r\n\t\t}\r\n \r\n\t\t\/\/ the method to create as a threadable method\r\n\t\tpublic static void RunThisMethod()\r\n\t\t{\r\n\t\t\tSingleton runsSingleton = Singleton.Instance;\t\t\r\n\t\t\tfor (int i =0; i < 10; i++)\r\n\t\t\t{\r\n\t\t\t\trunsSingleton.theValue++;\r\n\t\t\t\tConsole.WriteLine(\"RunThisMethod Singleton value \" + runsSingleton.theValue);\r\n\t\t\t\tThread.Sleep(45);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}<\/pre>\n<p>here is my output, as you can I am getting the singleton value incrementing, which is what should be happening.<\/p>\n<pre lang=\"csharp\">\r\nMain Singleton value 1\r\nRunThisMethod Singleton value 2\r\nRunThisMethod Singleton value 3\r\nRunThisMethod Singleton value 4\r\nMain Singleton value 5\r\nRunThisMethod Singleton value 6\r\nRunThisMethod Singleton value 7\r\nMain Singleton value 8\r\nRunThisMethod Singleton value 9\r\nRunThisMethod Singleton value 10\r\nMain Singleton value 11\r\nRunThisMethod Singleton value 12\r\nRunThisMethod Singleton value 13\r\nMain Singleton value 14\r\nRunThisMethod Singleton value 15<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>As from one of my previous posts about threading in csharp (c#), well with using Singletons you can use similar data between two different threads running. In basics a singleton is when you want to have a single class instance so that you can share this class so that every time that you use it &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/08\/17\/threads-singleton\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">threads &#8211; singleton<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[274,199],"class_list":["post-1164","post","type-post","status-publish","format-standard","hentry","category-c_sharp","tag-singleton","tag-thread"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1164"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1164\/revisions"}],"predecessor-version":[{"id":1165,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1164\/revisions\/1165"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}