News for hackers. Stuff that matters.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by cdoxsey on Tuesday January 28, 2020 @12:24PM (#22171542)
You have two choices when it comes to utilizing multiple cores: threads and processes. Threads are hard, but sharing data between processes is hardly any easier.

Maybe they're a bad idea, but these days you have no choice but to learn how to use threads. AMD's run-of-the-mill processors have 16 cores, Intel's 8. Servers have lots more. Heck even your iPhone has 6.

The clock rate on CPUs isn't getting any better. It's just more cores from here on.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by marcosdumay on Tuesday January 28, 2020 @02:56PM (#22173436)
It being hard to share memory is a feature, not a bug.

Threads make any single thing on your program mutable without your direct control. Processes keep the mutability scoped into a few hard to extend areas.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by FartyMcFarter on Tuesday January 28, 2020 @05:29PM (#22174808)
> Threads make any single thing on your program mutable without your direct control.

No they don't. Threads don't mutate random variables by themselves, you need actual code that does the mutation (whether it's running on a separate thread or not).

I mean, how is that statement different from "calling other functions makes any single thing on your program mutable"?

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by marcosdumay on Wednesday January 29, 2020 @12:17PM (#22183062)
> how is that statement different from "calling other functions makes any single thing on your program mutable"?

On those languages where functions mutate things, that's basically true. But it's much more common that functions can only mutate global variables, and people keep those in low numbers, exactly for that reason. Actually, replace "functions" with "methods" and you will get into one of the largest flaws of OOP.

But anyway, mutability is much less of a problem outside of concurrent code.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by dirtydroog on Tuesday January 28, 2020 @04:30PM (#22174260)
'const' all the things.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by jovial_cavalier on Tuesday January 28, 2020 @12:29PM (#22171595)
He says at the end that if you want true concurrency, use threads. But the point is that many people use threads where concurrency is not a requirement, or even desirable.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by chrisseaton on Tuesday January 28, 2020 @02:12PM (#22172995)
> But the point is that many people use threads where concurrency is not a requirement, or even desirable. reply

Why would you write a program using threads if you don't require concurrency? The only purpose of threads is to achieve concurrency. Can you give some examples?

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by sgerenser on Tuesday January 28, 2020 @02:37PM (#22173235)
Not the person you were asking the question to, but I can give you tons of examples. This is particularly true in the embedded space or people who write C or C++ that doesn’t have any standard event library. Basically, threads are a lowest-common denominator way to do blocking operations without stalling everything. I’ve seen a lot of code where there’s various threads running at different intervals (low priority background thread that sleeps for 1 second then wakes up to do stuff, another thread that does nothing but blink an LED, etc.) But in reality, in most non-cpu bound workloads, using an event loop would make writing this kind of code much easier and you woulnd’t ever have to worry about mutexes, semaphores, deadlocks, etc. As a C++ programmer, using Qt (even for non-UI stuff) with its event loop is just so much easier than spinning up threads just to call a network endpoint.

That being said, threads definitely have their place and are the only real way to take advantage of all the power offered by modern multi-core CPUs (well that and multi-process but that’s not really any easier to get right). But for the basic stuff I think running 80 threads when your app is only using 5% cpu is insane (something I see a lot in the C++ code I’m exposed to).

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by chrisseaton on Tuesday January 28, 2020 @02:40PM (#22173264)
Aren't all your examples still examples of concurrency?

> way to do blocking operations without stalling everything

Switching between multiple tasks doing IO - that's concurrency isn't it?

> various threads running at different intervals (low priority background thread that sleeps for 1 second then wakes up to do stuff

Different tasks ready to run and switching between them as needed - that's concurrency again isn't it?

Not really sure what everyone else in this thread is seeing that I'm not.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by sgerenser on Tuesday January 28, 2020 @05:14PM (#22174690)
OK, sure those are actually examples of concurrency. I was thinking of concurrency more along the lines of multiple threads of execution doing useful work at the same time, which threads are actually good at doing but these examples are not that.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by justincredible on Tuesday January 28, 2020 @03:19PM (#22173633)
The confusion might be due to everyone using concurrency to mean concurrency and/or parallelism.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by plapetomain on Tuesday January 28, 2020 @02:29PM (#22173156)
Think of an object oriented system. You can have a thread per object at the extreme where each object has its own thread/queue to handle messages. For most cases with synchronous calls you’re not really getting any concurrency.

Maybe it’s hard to imagine now, but in the 80s and 90s there were people that pushed this sort of architecture with a straight face. Even if not this extreme the idea of using threads for componentization rather than a focus on concurrency..which was possibly a side benefit was very much a thing (think COM/CORBA))

Hence why many articles like this and Ousterhout from the 90s, etc saying it was idiotic.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by aidenn0 on Tuesday January 28, 2020 @03:17PM (#22173611)
I worked on an embedded system with 1 thread per object and it was a very good ratio of code-to-expressivity (both when being used, and the implementation of the system). Each logical hardware port had its own thread, and was interacted with solely through message passing.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by brandmeyer on Tuesday January 28, 2020 @06:39PM (#22175414)
This is pretty much exactly the kind of robust system architecture that Erlang advocates employ.

In embedded systems, our kernels and threads are lightweight enough that we can go very fine-grained without paying a steep context-switching penalty. I'm not convinced that the penalty in Linux is all that high, either. Its only when you're going after the C10K (or C1M?) problem that you start to notice.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by aidenn0 on Tuesday January 28, 2020 @07:47PM (#22175955)
> In embedded systems, our kernels and threads are lightweight enough that we can go very fine-grained without paying a steep context-switching penalty.

Right, this system would run through the entire runlist at several kHz when idle and 0.5-1kHz under load on a PowerPC 405 that ran at about 200MIPS. Our shortest deadline was 10ms so it was plenty fast enough. Context switch was swapping out 12 machine words.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by jovial_cavalier on Wednesday January 29, 2020 @02:32PM (#22184837)
>why would you write a program to use threads if you don't need concurrency?

... exactly. There is no good reason. That's what I'm saying the point of the article is. However, this doesn't mean that people don't do it. He is saying that the tasks that people typically use threads to solve can actually be solved with an event loop and handlers, thus eliminating all of the messy issues with shared state, race conditions, etc. that true concurrency introduces.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by jandrese on Tuesday January 28, 2020 @02:31PM (#22173184)
I think that is referring to the case of "select() is too confusing, I'll use threads instead so I won't have to think about it."
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by chrisseaton on Tuesday January 28, 2020 @02:35PM (#22173214)
I think I'd still call that concurrency - you want multiple continuations to preserve state between IO calls.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by mjpuser on Tuesday January 28, 2020 @05:09PM (#22174639)
I think that programmers are quick to jump to concurrency when they want to make something performant as opposed to using other structures, like a cache, or researching other possible solutions, like the HN favorite bloom filter, or other algorithm.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by didibus on Tuesday January 28, 2020 @02:01PM (#22172860)
That said, threads don't give you quite the boost you might be hoping for. Even with things that can parallelize well, it seems at around 8 or 12 threads you start to hit a wall.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by dirtydroog on Tuesday January 28, 2020 @04:33PM (#22174290)
How so? How many cores on the machine? Maybe you have some false-sharing, maybe thermal throttling is kicking in.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by didibus on Thursday January 30, 2020 @01:29PM (#22193853)
Some of what I heard is memory access bottlenecks and cache coherency bottlenecks.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by redisman on Tuesday January 28, 2020 @01:12PM (#22172251)
It's good but not quite as good. 16 core is the high end of consumer CPUs, mid-range is probably more like 6 or 8 cores.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by ychen306 on Tuesday January 28, 2020 @11:37PM (#22177488)
I think he's referring to threads as a programming model, not the physical implementation.