Are threads still a menace?
<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>
<blockquote><p>
In 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>
<p>1. Portability to Windows, which doesn&#8217;t have an equivalent of fork(2)</p>
<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>
<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?
</p></blockquote>
<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>
<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>
<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>
