TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Why Threads are a Bad Idea (for most purposes) (1995) [pdf]

175 pointsby erwanalmost 8 years ago

23 comments

skybrianalmost 8 years ago
The morning paper had a nice set of blog posts about this:<p>Why they&#x27;re equivalent (duals): <a href="https:&#x2F;&#x2F;blog.acolyer.org&#x2F;2014&#x2F;12&#x2F;08&#x2F;on-the-duality-of-operating-system-structures&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.acolyer.org&#x2F;2014&#x2F;12&#x2F;08&#x2F;on-the-duality-of-operat...</a><p>Why threads are a bad idea: <a href="https:&#x2F;&#x2F;blog.acolyer.org&#x2F;2014&#x2F;12&#x2F;09&#x2F;why-threads-are-a-bad-idea&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.acolyer.org&#x2F;2014&#x2F;12&#x2F;09&#x2F;why-threads-are-a-bad-id...</a><p>Why events are a bad idea: <a href="https:&#x2F;&#x2F;blog.acolyer.org&#x2F;2014&#x2F;12&#x2F;10&#x2F;why-events-are-a-bad-idea&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.acolyer.org&#x2F;2014&#x2F;12&#x2F;10&#x2F;why-events-are-a-bad-ide...</a><p>Unifying events and threads (in Haskell): <a href="https:&#x2F;&#x2F;blog.acolyer.org&#x2F;2014&#x2F;12&#x2F;11&#x2F;a-language-based-approach-to-unifying-events-and-threads&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.acolyer.org&#x2F;2014&#x2F;12&#x2F;11&#x2F;a-language-based-approac...</a><p>Unifying events and treads (in Scala): <a href="https:&#x2F;&#x2F;blog.acolyer.org&#x2F;2014&#x2F;12&#x2F;12&#x2F;scala-actors-unifying-thread-based-and-event-based-programming&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.acolyer.org&#x2F;2014&#x2F;12&#x2F;12&#x2F;scala-actors-unifying-th...</a>
评论 #14551753 未加载
评论 #14549858 未加载
评论 #14550198 未加载
评论 #14550428 未加载
atemerevalmost 8 years ago
Yes, might have worked in 1995. Now, however, when even your lowly phone has 4 (or more) processor cores and a full-fledged GPU...<p>Learn concurrent programming techniques — or perish. Threads and sync primitives are low-level, but important, and you have to understand them to figure out what compromises and biases were taken in higher-level models.<p>And, frankly, it isn&#x27;t that bad (debugging existing code is bad, but playing with monitors and semaphores and critical sections is easy, until code is small and isolated).
评论 #14549298 未加载
评论 #14549861 未加载
评论 #14549503 未加载
评论 #14551244 未加载
评论 #14550220 未加载
评论 #14550786 未加载
notacowardalmost 8 years ago
Note that it&#x27;s from 1995. Back then, many people still thought that machines with multiple processors were exotic things of little interest even in the enterprise. That list most notably included one Linus Torvalds. It also included OP author John Osterhout, who should also have known better - even more so, since Stanford was one of the places where such things were not so exotic. He even says, right up front, that threads still have their uses when you need true CPU concurrency. Now that&#x27;s a common case. Generalizing from this presentation to the current day is probably a worse idea than threads ever were.
评论 #14548203 未加载
评论 #14549507 未加载
评论 #14548046 未加载
theszalmost 8 years ago
John Osterhout is a creator of Tcl, which embraces event-driven model for programming. I think it gives more perspective into his opinion.<p>That said, Tcl was one of the first scripting languages which got very nice thread model - see AOLServer [1].<p>[1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;AOLserver" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;AOLserver</a><p>We used Tcl threads in one of our programs to control various hardware things while main UI responded to events sent from the threads. Everything worked very well, especially for a program in scripting language like Tcl.
milesvpalmost 8 years ago
Twenty years later this seems to still be good advice. Martin Thompson talks about this in his Mechanical Sympathy talk.<p>He says the first thing he does, as a performance consultant, is turn off threading. Claims that&#x27;s often all he needs to achieve the desired improvements...<p>It&#x27;s a good talk, I highly recommend it.<p><a href="https:&#x2F;&#x2F;www.infoq.com&#x2F;presentations&#x2F;mechanical-sympathy" rel="nofollow">https:&#x2F;&#x2F;www.infoq.com&#x2F;presentations&#x2F;mechanical-sympathy</a>
评论 #14549790 未加载
Animatsalmost 8 years ago
The bad idea is taking a threaded language and retrofitting events, which Python is doing. This results in an even worse mess. Python now has two kinds of blocking, one for threads and one for events. If an async event blocks on a thread lock, the program stalls.<p>Or taking a event-driven language and retrofitting concurrency, which Javascript is doing. That results in something that looks like intercommunicating processes with message passing. That&#x27;s fine, but has scaling problems, because there&#x27;s so much per-process state.<p>Languages which started with support for both threads and events, such as Go, do this better. If a goroutine, which is mostly an event construct, blocks on a mutex, the underlying thread gets redispatched. There&#x27;s only one kind of blocking.
评论 #14553634 未加载
outworlderalmost 8 years ago
From a Linux perspective, threads and processes are essentially the same construct. The major difference is the set of flags that are passed when the process is created. Oversimplifying, if shared memory is requested, then it&#x27;s a thread. Otherwise, it&#x27;s a process. Meaning forking servers are multi-threaded.<p>On other operating systems, specifically those starting with the letter W, there&#x27;s a major distinction. There are other constructs as well, such as &quot;fibers&quot;.<p>Now, today&#x27;s world is different from what it was in 1995. We used to have a single core, so threads and multiple processes were only a logical construct. Now, we have multiple cores, so we shold, at a bare minimum spawn multiple processes&#x2F;threads. What&#x27;s running inside them can then be debated as if it were 1995.
gensalmost 8 years ago
Obligatory CSP[0] reference.<p>There are only a handful of examples, that i can think of, where threading(multiprocessing, concurrency, and other names for it) is useful.<p>[0] <a href="http:&#x2F;&#x2F;www.usingcsp.com&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.usingcsp.com&#x2F;</a>
vyodaikenalmost 8 years ago
The key point is unstructured shared data is a source of errors in a threaded program.
cjensenalmost 8 years ago
If I had a dollar for every time I heard &quot;thread programming is hard.&quot;<p>I&#x27;ve programmed using threads for 23 years. I&#x27;ve never had a non-trivial debug issue caused by trouble using semaphores, mutexes, and shared data. It&#x27;s no harder than writing a hash table or balancing a tree.
评论 #14550218 未加载
评论 #14551478 未加载
评论 #14550215 未加载
评论 #14550810 未加载
评论 #14550726 未加载
dgreenspalmost 8 years ago
I&#x27;d rather have real threads available in my language, and use shared state sparingly. Your N single-threaded processes have to talk to each other anyway, or at least to a master, and might even share memory through memory-mapping. Threads are just a tool, and they give you options. You can use them in a share-nothing way if you want.<p>As someone who grew up on the Java VM and started my start-up&#x2F;web career on it, I&#x27;ve always felt like Java programmers have a different relationship with threads than C programmers. Java gives you cross-platform, usable, debuggable native threads; it basically makes them free if you want them. In C&#x2F;C++, on the other hand, threads are a library, and using them is a grungy affair. If you grew up on Rails, meanwhile, threads don&#x27;t exist (&quot;when you say worker thread, do you mean worker process? I&#x27;m confused&quot;).<p>Node.js was created by C programmers and launched with a lot of anti-thread propaganda, much like the link. They equated threads with shared state, and also said threads were too memory inefficient to be practical for a high-performance server (they meant that holding a native thread for every open connection would require too much stack space, which is true, but that&#x27;s not what they said).
评论 #14550855 未加载
woliveirajralmost 8 years ago
&gt; Where threads needed, isolate usage in threaded application kernel: keep most of code single-threaded.<p>This is the point where performance tops: each CPU is filled with operations, and operations that don&#x27;t need to wait for the result of other threads.
sbovalmost 8 years ago
I&#x27;m sure many people don&#x27;t think this applies to them because they don&#x27;t use threads. However, in the modern day, replace &quot;thread&quot; with &quot;process&quot; and &quot;memory&quot; with &quot;database&quot;, and many web applications have very similar problems. They just never actually manifest because of the small number of requests per second.
评论 #14549040 未加载
EGregalmost 8 years ago
I agree. The actor model where actors can be scheduled on any thread is the best (Erlang, Goroutines). Second best is the node.js model of single threaded evented programming.
评论 #14547890 未加载
评论 #14548643 未加载
评论 #14547960 未加载
评论 #14547953 未加载
评论 #14547836 未加载
评论 #14548122 未加载
评论 #14548759 未加载
评论 #14547829 未加载
joostersalmost 8 years ago
IMO, the main problem with using threads is that they are such an &#x27;all or nothing&#x27; approach to sharing data.<p>If you want to make use of multiprocessing, the traditional choice is either to use two separate processes (sharing nothing), or to use threads (and share everything). But for most tasks, these opposite ends of the spectrum are not what you need. There&#x27;s plenty of data and state in most programs that doesn&#x27;t need to be shared, and a huge source of threading bugs is through mistakenly altering some data that another thread was using.<p>The problem is that sharing partial state between processes is painful and many languages and OSs make it difficult to do. You have to play around with mmap() or other shared memory tools, and then pay great attention to not mix pointers or other incompatible data between the processes.
geezerjayalmost 8 years ago
There&#x27;s a link to a related discussion on HN entitled &quot;Why Events Are a Bad Idea (for high-concurrency servers) (2003)&quot;<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=14548487" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=14548487</a>
ilakshalmost 8 years ago
Threads have been useful for me in my latest experiment. Its a C++ application that runs mame_libretro dll (copies) each in their own thread, while a BASIC intepreter runs in another thread, and the main game engine (Irrlicht) runs in the main thread. Irrlicht isn&#x27;t multithreaded so I just put commands from the BASIC thread into an outgoing deque which I lock and unlock as I access it. Then there are mutexes for the video data.<p>I think that threads are definitely a bit tricky though since it is easy to mess up locking&#x2F;unlocking or not lock things necessary and if you do then you have debugging headaches. So when not needed they should be avoided I think.
faragonalmost 8 years ago
I can not agree more. Most people should not write threaded code at all.
kulu2002almost 8 years ago
Whatever... But Multithreaded systems are fun to design, develop and most importantly - troubleshoot... more harder the &#x27;real time&#x27;ness more the fun :)
dreamdu5talmost 8 years ago
Threads are a bad idea for the same reason manual handling of memory space is a bad idea. Languages should provide primitives that only allow for safe construction of expressions that are run concurrently by the runtime.
评论 #14548774 未加载
评论 #14548681 未加载
ythnalmost 8 years ago
What if I have a blocking function call (i.e. listening on a socket)? Seems like there is no choice but to put the blocking call in its own thread...
评论 #14549030 未加载
评论 #14549010 未加载
评论 #14549086 未加载
Kenjialmost 8 years ago
It&#x27;s the same with every tool: The more powerful it is, the worse the consequences are when it&#x27;s abused. That applies to programming in particular.
评论 #14547959 未加载
in9almost 8 years ago
Loved the programmer comparison slide. Is today&#x27;s python programmer 1995&#x27;s visual basic programmer? :D
评论 #14548404 未加载