{"id":4017,"date":"2011-12-21T11:04:06","date_gmt":"2011-12-21T16:04:06","guid":{"rendered":"http:\/\/esr.ibiblio.org\/?p=4017"},"modified":"2014-03-11T19:28:27","modified_gmt":"2014-03-11T23:28:27","slug":"are-threads-still-a-menace","status":"publish","type":"post","link":"http:\/\/esr.ibiblio.org\/?p=4017","title":{"rendered":"Are threads still a menace?"},"content":{"rendered":"<p>An interesting question showed up in my mailbox today.  So interesting that I think it&#8217;s worth a public answer and discussion:<\/p>\n<blockquote><p>\nIn chapter 7 of <cite>The Art of Unix Programming<\/cite>, you classified threads under the section &#8220;Problems and Methods to Avoid&#8221;. You also wrote that with the increased emphasis on thread-local storage, threads are looking more like a controlled use of shared memory. This trend has certainly continued; recent programming languages like D, Scala, and Go encourage the use of threads as mostly isolated lightweight processes with message passing. Observing this trend, I have often wondered, why not go all the way and use multiple OS processes? I can think of two reasons to use threads in this newer, controlled way rather than using full processes:<\/p>\n<p>1. Portability to Windows, which doesn&#8217;t have an equivalent of fork(2)<\/p>\n<p>2. Performance, particularly because message passing between real processes requires serialization and deserialization, whereas message passing within a process can be done with shared memory and (maybe) locks<\/p>\n<p>So what do you think? Are threads still a menace to be avoided in favor of full OS processes? Or has the situation improved since 2003?\n<\/p><\/blockquote>\n<p>I think it has, and I think you&#8217;ve very nearly answered your own question as to why. Bare threads were dangerously prone to deadlocks, livelocks, context-trashing, and various other sorts of synchronization screwups &#8211; so language designers set out to encapsulate them in ways that gave better invariants and locality guarantees without sacrificing their performance advantages. I think Scala&#8217;s transactional memory stands out as a particularly elegant stab at the problem.<\/p>\n<p>I don&#8217;t develop for Windows or communicate much with people who do, so I&#8217;m not equipped to judge how important Windows portability is in motivating these features.  But the performance issue you called out is real and quite alive on Unix systems.<\/p>\n<p>UPDATE: Matt Campbell, who has materialized in the comments here, send the original question and has given me permission to cite him.  Thanks for a good question!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An interesting question showed up in my mailbox today. So interesting that I think it&#8217;s worth a public answer and discussion: In chapter 7 of The Art of Unix Programming, you classified threads under the section &#8220;Problems and Methods to Avoid&#8221;. You also wrote that with the increased emphasis on thread-local storage, threads are looking&hellip; <a class=\"more-link\" href=\"http:\/\/esr.ibiblio.org\/?p=4017\">Continue reading <span class=\"screen-reader-text\">Are threads still a menace?<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-4017","post","type-post","status-publish","format-standard","hentry","category-software","entry"],"_links":{"self":[{"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=\/wp\/v2\/posts\/4017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4017"}],"version-history":[{"count":5,"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=\/wp\/v2\/posts\/4017\/revisions"}],"predecessor-version":[{"id":5431,"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=\/wp\/v2\/posts\/4017\/revisions\/5431"}],"wp:attachment":[{"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4017"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}