{"id":41573,"date":"2003-12-09T10:00:00","date_gmt":"2003-12-09T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2003\/12\/09\/why-you-should-never-suspend-a-thread\/"},"modified":"2003-12-09T10:00:00","modified_gmt":"2003-12-09T10:00:00","slug":"why-you-should-never-suspend-a-thread","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20031209-00\/?p=41573","title":{"rendered":"Why you should never suspend a thread"},"content":{"rendered":"<p>\nIt&#8217;s almost as bad as terminating a thread.\n<\/p>\n<p>\nInstead of just answering a question, I&#8217;m going\nto ask you the questions and see if you can come\nup with the answers.\n<\/p>\n<p>\nConsider the following program, in (gasp) C#:\n<\/p>\n<pre>\nusing System.Threading;\nusing SC = System.Console;\nclass Program {\n  public static void Main() {\n    Thread t = new Thread(new ThreadStart(Program.worker));\n    t.Start();\n    SC.WriteLine(\"Press Enter to suspend\");\n    SC.ReadLine();\n    t.Suspend();\n    SC.WriteLine(\"Press Enter to resume\");\n    SC.ReadLine();\n    t.Resume();\n  }\n  static void worker() {\n    for (;;) SC.Write(\"{0}\\r\", System.DateTime.Now);\n  }\n}\n<\/pre>\n<p>\nWhen you run this program and hit Enter to suspend, the program hangs.\nBut if you change the worker function to just &#8220;for(;;) {}&#8221; the program\nruns fine.\nLet&#8217;s see if we can figure out why.\n<\/p>\n<p>\nThe worker thread spends nearly all its time calling System.Console.WriteLine,\nso when you call Thread.Suspend(), the worker thread is almost certainly\ninside the System.Console.WriteLine code.\n<\/p>\n<p>\nQ: Is the System.Console.WriteLine method threadsafe?\n<\/p>\n<p>\nOkay, I&#8217;ll answer this one:  Yes.  I didn&#8217;t even have to look at any\ndocumentation to figure this out.  This program calls it from two\ndifferent threads without any synchronization, so it had better be\nthreadsafe or we would be in a lot of trouble already even before\nwe get around to suspending the thread.\n<\/p>\n<p>\nQ: How does one typically make an object threadsafe?\n<\/p>\n<p>\nQ: What is the result of suspending a thread in the middle of a threadsafe\noperation?\n<\/p>\n<p>\nQ: What happens if &#8211; subsequently &#8211;\nyou try to access that same object (in this case,\nthe console) from another thread?\n<\/p>\n<p>\nThese results are not specific to C#. The same logic applies\nto Win32 or any other threading model.\nIn Win32, the process heap\nis a threadsafe object, and since it&#8217;s hard to do very much in\nWin32 at all without accessing the heap, suspending a thread\nin Win32 has a very high chance of deadlocking your process.\n<\/p>\n<p>\nSo why is there even a SuspendThread function in the first place?\n<\/p>\n<p>Debuggers use it to freeze all the threads in a process while you\nare debugging it.  Debuggers can also use it to freeze all but one\nthread in a process, so you can focus on just one thread at a time.\nThis doesn&#8217;t create deadlocks in the debugger since the debugger\nis a separate process.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s almost as bad as terminating a thread. Instead of just answering a question, I&#8217;m going to ask you the questions and see if you can come up with the answers. Consider the following program, in (gasp) C#: using System.Threading; using SC = System.Console; class Program { public static void Main() { Thread t = [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-41573","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It&#8217;s almost as bad as terminating a thread. Instead of just answering a question, I&#8217;m going to ask you the questions and see if you can come up with the answers. Consider the following program, in (gasp) C#: using System.Threading; using SC = System.Console; class Program { public static void Main() { Thread t = [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/41573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=41573"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/41573\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=41573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=41573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=41573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}