News for hackers. Stuff that matters.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by travisgriggs on Tuesday January 28, 2020 @03:28PM (#22173712)
> Doesn't change the fact that it's an extremely bad fit for the human brain to think about parallelism.

I'm curious why you feel this way? Certainly anything with multiple (more than one) thread is a lot harder than single threaded. Given.

But I think the human experience is rich with real world counterparts that can make multithreaded programming "natural" (not necessarily easy, there's a difference). Basically any process in real life you do that involves collaborating with multiple people is a collaborative multi-threaded process. When forced to grapple with multi-threaded solutions, I often ask myself, "if I had a room full of people working on this problem, what would have to be in place to make the operation flow smoothly?" For me, this anthropomorophisation of the process makes it a "good fit" for how my brain is used to solving lots of real world problems.

On the flip side, I haven't had a lot of luck finding real world experiences that I can model coroutines/async/etc on. So they may be ultimately easier, but if our rubrik for "fit for the human brain to think about" is what the wealth of human experience has evolved our brain to handle well, I'm less convinced.

But I like learning new things. Maybe I just haven't seen the lightbulb yet. Help me see your point of view?

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by pdimitar on Tuesday January 28, 2020 @04:10PM (#22174055)
> On the flip side, I haven't had a lot of luck finding real world experiences that I can model coroutines/async/etc on.

I feel we might be having too incompatible perceptions of the world but I'll try.

Every communication (human or otherwise) is exactly actors with message inboxes. We the humans can't react to 3 incoming conversations at the same time. Our brain queues up what it hears or reads and then responds serially, one by one (not necessarily in order of arrival). Basically an actor with a message inbox, like in Erlang/Elixir.

The way you assert multithreading mimics the real world to me is not convincing. One possible example in favour of your point of view I could think of is probably a table full of food and 20 people reaching and grabbing from it at the same time. But even then, two people cannot successfully get the same piece of meat. So not a true multithreading in the sense of N readers/writers competing for the same resource; it's more like a shared memory area where parallel writes are okay as long as all writers reference different parts of it.

Sure, a lot of stuff collides in real time without synchronisation out there, that much is true. But you likely noticed we the people don't cope with the Universe's chaos very well and rarely manage to grasp it properly. So is it really a good way to model deterministic and non-chaotic systems with programming? To me it's not.

I am curious why do you even think multithreading mimics the real world at all. Elaborate, if you are willing? (We might think of different definitions of multithreading in this instance.)

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by zzzcpan on Tuesday January 28, 2020 @03:33PM (#22173752)
> Basically any process in real life you do that involves collaborating with multiple people is a collaborative multi-threaded process.

That would be actors or processes communicating with each other, not a collaborative multi-threaded process.

Real world has pretty much no natural notion of threads, only of actors.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by blackflame on Tuesday January 28, 2020 @05:09PM (#22174647)
Have you ever seen those assembly lines where a bunch of people sit around the conveyer belt and each one grabs a product, makes an addition, and sets it back down? That's fairly close.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by vishnugupta on Tuesday January 28, 2020 @07:06PM (#22175644)
Each person on an assembly line operates independent of the rest, there’s no “coordination” as such. A person picks up an incoming object, transforms it and sets it down. For that person, for all practical purposes, the rest of the people working on conveyor belt don’t even exist. I suppose that (not having to coordinate) played a key role in the popularity of conveyor systems. It’s simple enough to debug, address bottlenecks etc.

The closest s/w equivalent I could think of is Unix pipes. When I do “cat foo.csv | sort | uniq -c”, cat/sort/unique aren’t coordinating with each other, each takes the content from stdin, transforms and place it in stdout.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by blackflame on Thursday January 30, 2020 @04:44AM (#22190149)
That's what optimum parallelism is. They are collaborating to process N number as many items at the same time in the most optimum way. They also are coordinating to prevent someone from grabbing the same item or placing the same item in the same place. The conveyor belt itself is a ring buffer.

The assembly line principle can also be applied to the concept of threads. Workers collaborate by handing items off to one or more workers(threads) down the line. Thread pools are just micromanagers constantly reassigning the poor workers, but at least the make a lot of friens collaborating.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by zo1 on Tuesday January 28, 2020 @04:02PM (#22173986)
> "I'm curious why you feel this way? Certainly anything with multiple (more than one) thread is a lot harder than single threaded. Given."

Not the OP, but in my experience most devs are bad enough dealing with state and flow in single-threaded code already. Including myself. But I've at least accepted that and try to mitigate it by avoiding things that make it worse (like threads). Meanwhile, I watch people around me doing code by spaghetti (throwing it on a wall and seeing if it sticks). The better devs don't apply that to their code, but still end up applying it to their "reasoning" about a problem domain, especially a business one.

E.g.: "Screw it, if this variable that I need is null I'll just return a False in this function I'm writing" ... without ever thinking "why" this variable might be null and how that explanation impacts the problem they're solving. Nevermind the fact that they're fully OK with propagating such a broken state further into the program with their False response. Now some poor other dev has to figure out why this function is returning a false result. Odds are they'll do the same thing and now we've doubled the amount of bad states.

Adding in even more complexity in terms of being able to "predict" code state by adding threads is a nightmare. I would most certainly protest and heavily advise against anyone using threads unless it's really really the thing that's needed to solve the problem. Otherwise, it's just adding more a lot more complexity for very little benefit if any at all.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by derefr on Tuesday January 28, 2020 @04:00PM (#22173969)
> Basically any process in real life you do that involves collaborating with multiple people is a collaborative multi-threaded process.

I would argue that the code equivalent of people collaborating in the real world is multiple processes with IPC (or even further afield, multiple nodes in a distributed system), not threads. You don’t share memory with other human beings. Imagine how productive you could be if you did!

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by harikb on Tuesday January 28, 2020 @04:22PM (#22174184)
If we read other people’s (say a manager, who is giving work) mind and just did that, we would be in the exact mess we are with shares mutable state.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by SeekingMeaning on Tuesday January 28, 2020 @05:30PM (#22174817)
Imagine reading someone else’s mind, only to discover that they are reading yours... Mind-stack overflow?
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by derefr on Tuesday January 28, 2020 @06:54PM (#22175540)
for certain classes of problems, sure. But how about “embarrassingly parallel” jobs (e.g. weeding a garden) or even “idempotent OLTP” jobs (e.g. studying several subjects at once)?

(Technically these aren’t best expressed as SHM but rather as a tuplespace or greedy worker-pool abstraction, but they can be achieved starting off with “just” an SHM threading primitive, so they count, I think.)

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by nybble41 on Wednesday January 29, 2020 @02:10PM (#22184592)
> You don’t share memory with other human beings.

This really depends on where you draw the boundaries. In a computer everything is "memory" of one kind or another, but that doesn't necessarily correspond to human memory. The analogy works if you think of the "actor" as merely the thread's context and dedicated working space (stack), with everything else being part of the surrounding environment. In the real world we don't have anything akin to process separation, except by convention. Even the body itself can be directly manipulated by others. The convention we use for communication between individuals looks more like a message-passing system, but physical manipulation of matter is more like operating on data in shared memory. There is no physical law preventing someone else from coming along and messing with an object I happen to be holding any more than the rules of a computer prevent one thread from messing with a data object held by another thread.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by lmm on Tuesday January 28, 2020 @09:25PM (#22176587)
Collaborating humans have nothing like the problems that require memory barriers. We rarely have anything that behaves like shared memory - shared physical objects might come close, but a physical object is inherently de facto protected by a mutex.

Async, actor-like processes are everywhere in the real world - "something comes into my inbox, I do my piece of work on it, then send it to their inbox". Or "I'll send this off, do something else for now, and then go back to working on that when I get their reply".

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by bnegreve on Wednesday January 29, 2020 @12:16AM (#22177677)
When you write a sequential program, you are essentially specifying one order of instructions that will produce the correct output. With parallel program, you need to consider every possible orders and add the correct synchronization to prevent unwanted orders from happening. That's the difficult part.

As a programming model, threads don't really help you to achieve this. When there's a bug, your only option is pretty much to think very hard and try to figure out what happened. (Or to use gdb with scheduler locking, which is about as bad.)

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by blackflame on Tuesday January 28, 2020 @05:06PM (#22174619)
There are more than one way to parallelize things as well. For example you could have and assembly line methodology where different threads are working on different stages at the same time. Using a common shared threadpool, as one stage slows down, it receives more workers to speed it up. Using producer/consumer queues to designate when data is being passed from one threads control to the other eliminates a lot of multi-threading bugs if this architecture is appropriate.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by Taniwha on Tuesday January 28, 2020 @03:53PM (#22173919)
I think it's a way of thinking - I was an OS hack - Unix device drivers/etc early in my career, I could think about interrupt races/etc but it was hard, I took a detour into designing chips for a decade where EVERYTHING is parallel, by the time I got back to kernel work (Linux was around by then) I found that stuff that had been hard was now obvious.

In short if it's not a good fit for your brain, change your brain!

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by bakul on Tuesday January 28, 2020 @06:31PM (#22175364)
H/W components don't share state, which is why H/W parallelism easier. Where they do interact with each other, we have to either design fully async logic or worry about metastability. In contrast all kernels are basically all about sharing state. Even in user code and even when, a language such as Go provides channels, a lot of people seem to use shared state (and mutexes and locks).

I am curious as to how your h/w experience makes it easier hacking on the linux kernel....

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by Taniwha on Tuesday January 28, 2020 @06:42PM (#22175433)
There's lots of shared state, every flop is shared state, and mostly here I'm talking about synchronous parallelism in pipelines - yes we do have to deal with metastability too, and that's a whole other level of evil so that people avoid it where possible and spent lots of brainpower mitigating it (metastability is so evil you can't completely make it go away, just make it extremely unlikely).

Go is sort of like saying "we're not going to deal with parallelism directly, just throw everything into fifos to talk to other places" mostly you can't afford to waste gates like that in real hardware (there are places where it makes sense - I've built a lot of display controllers).

I think the big difference is the way you end up thinking about time and timing holes in code (dealing with interrupts), after building gates for a while i think you tend to see the timing holes more easily

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by ergothus on Tuesday January 28, 2020 @03:59PM (#22173959)
Take decade to learn threads? Then expect other devs to do the same? I think that proves the point that threads aren't a good fit, even if we are technically capable of it.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by Taniwha on Tuesday January 28, 2020 @04:02PM (#22173987)
No, it didn't take a decade, that was just how long I did it for (before it got boring) - I just mean that you have to learn to think about parallelism in a different way, we train hardware engineers to do this but not software ones
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by pdimitar on Tuesday January 28, 2020 @04:12PM (#22174068)
So it eventually got boring for you. So... threads are a bad mental model for our brains? :P

Not sure what you mean by your statement that hardware engineers are trained to think differently about it. I am not sure there's much overlap between HW and SW engineers in terms of parallelism modelling. Do you have a few examples?

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by Taniwha on Tuesday January 28, 2020 @05:05PM (#22174603)
No, chip making got boring - essentially it's a month a year of doing fun creative stuff and 11 months a year making sure that it makes timing and is perfect before you tape out. When you're coding on the other hand you can get something new working every day. I chose to go back to software.

However having said that I find myself designing silicon again, change is good for the brain I guess

Hardware people tend to think in terms of netlists and pipelining so they have to worry about parallism on every clock

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by baggy_trough on Tuesday January 28, 2020 @03:31PM (#22173731)
The greatest programmers in the world cannot write bug free C level threading code. It is a task beyond human capabilities.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by mypalmike on Tuesday January 28, 2020 @05:28PM (#22174799)
I'm not one of the greatest programmers in the world, but I don't find C threads to be particularly hard to deal with in a robust way.

Threads in C are hard if you have a sloppy codebase with global variables everywhere and a general lack of structure and abstraction. Unfortunately, this describes many many C codebases.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by ringzero on Tuesday January 28, 2020 @04:20PM (#22174166)
I don’t follow - are you asserting that, by contrast, the greatest programmers in the world can write bug-free single-threaded code?
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by lmm on Tuesday January 28, 2020 @09:17PM (#22176538)
I would argue that a moderately talented programmer can write code that does exactly what they think it is supposed to do (whether you call that "bug-free" is a question of semantics) if the have a language and type system (or equivalent) that allows them to express their requirements, and they don't step outside what they can express that way (i.e. they don't write code when they can't express what its semantics should be). Languages/compilers that can do that for multithreaded code are extremely niche.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by ken on Tuesday January 28, 2020 @08:20PM (#22176173)
For all intents and purposes, Knuth has. He's certainly more careful than most but I don't think he's unique among all of humanity here.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by pshc on Tuesday January 28, 2020 @04:40PM (#22174347)
I think the implication is to stop using C.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by pdimitar on Tuesday January 28, 2020 @04:46PM (#22174402)
I'd interpret it to stop using the pthreads model of the parallel coding, in general. Because the pthreads model exists and is used in many programming languages.

But stopping to use C is a good start (for whoever has the choice)!

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by nineteen999 on Tuesday January 28, 2020 @07:03PM (#22175620)
Maybe the functional programming/managed code fans should step up to the plate and rewrite the operating systems, desktop environments, and hundreds of thousands of command line tools etc that have all been implemented in C or C++.

Not as toy, proof-of-concepts. Fully fledged replacements for all that stuff that can be used in our our daily work. Build distros of the stuff so that we don't have to pick and choose this stuff and replace the bits of our systems in a piecemeal fashion.

They could show us how it's done instead of talking endlessly about it in online forums. So let's face it, it's never going to happen.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by pdimitar on Tuesday January 28, 2020 @07:16PM (#22175718)
Hm, where did I say anything about FP?

As for managed code, it's being used with huge success in a lot of places (not in OS-es or drivers, yes) but that's quite the huge topic by itself.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by nineteen999 on Tuesday January 28, 2020 @07:50PM (#22175982)
I never implied you did, they are merely one of the two major major groups of programmers I see consistently deriding C based infrastructure that presumably enables their paychecks to a large extent.

I'm not defending C, I'm merely sneering at its noisy detractors who spend more time complaining about it than supplanting it.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by pshc on Tuesday January 28, 2020 @11:57PM (#22177578)
Well, people are working on it. For example, what's been happening in Firefox. C/C++ has a head start in the millions of person-hours.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by nineteen999 on Thursday January 30, 2020 @12:43AM (#22189139)
One could argue as a counterpoint that the Rust community (and all the other language communities) have millions of person-hours (and lines of of C/C++ open source code) to reference as they RIIR, a luxury the C/C++ community didn't really have, much of what they've built since Linux appeared was developed from scratch. Outside of 386BSD, there wasn't a lot of unencumbered UNIX source code available to them.

Perhaps as the Rust community grows in size and momentum this will happen more. Right now it seems like a bit of a manpower problem.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by yawaramin on Tuesday January 28, 2020 @07:15PM (#22175705)
@nineteen999: that all happened a long time ago. Study your history: https://en.wikipedia.org/wiki/Lisp_machine
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by nineteen999 on Tuesday January 28, 2020 @10:02PM (#22176868)
I mean to replace the ones that we use today. Not the ones we used 30 years ago. General purpose operating systems. Perhaps with a distribution we can download for free and install on commodity computing hardware, and actually use today for our primary areas of work/interest.

Note the section 1.7 in your own link ("End of the Lisp machines"). The Lisp software ecosystem was never as rich, broad and varied as the C/C++ software ecosystem is today, before it died.

BTW, I only stumbled across your comment by accident, since you replied to the parent poster instead of me. Parenthesis mismatch?

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by yawaramin on Wednesday January 29, 2020 @08:52AM (#22180402)
HN thread depth reply limit!
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by nullValue on Tuesday January 28, 2020 @09:22PM (#22176569)
I respectfully disagree. I have a embedded multi-threaded 'C' program running in over 11k+ retail stores in the USA right now. It's been handling multiple client requests to a Sqlite DB since 2007 without any issues. This product has made my company a lot of revenue. The secret to using threads is all in the design. Don't share resources between threads (I only had one shared resource for 50+ threads guarded by a semaphore).

Cheers.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by pshc on Tuesday January 28, 2020 @11:49PM (#22177539)
That is a decent anecdote. Well, with respect, allow me to revise and qualify my read of @baggy_trough's comment:

It's perfectly fine to continue using an existing C codebase for a program, not exposed to the public internet, that's maintained by a focused group of maintainers. But on the other side of this spectrum, for large exposed projects like OpenSSL, Chromium, or even Linux, C/C++ has become risky.

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by gameswithgo on Tuesday January 28, 2020 @04:10PM (#22174058)
Yet sometimes a simpler abstraction can't do the job well enough. Sometimes you have to do the hard thing.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by pdimitar on Tuesday January 28, 2020 @04:14PM (#22174089)
IMO nobody is arguing about the sometimes part. Many programmers out there can't use Erlang/Elixir or Rust, or any other tech that makes writing parallel code much more deterministic and safe. I am aware of that and I have a deep respect for the programmers in the trenches who fight to produce bug-less parallel code in C.

My argument at least is that, given the choice, there exist much more productive ways to make your employer money than to try and learn nuclear phys... I mean multithreaded programming. :-)

Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by lmm on Tuesday January 28, 2020 @09:20PM (#22176551)
Citation needed. I've yet to see a real business problem that couldn't be solved with a simpler, safer concurrency model and a bit of ingenuity.
Re:Threads Are a Bad Idea for Most Purposes (1995) [pdf] by travisgriggs on Tuesday January 28, 2020 @03:40PM (#22173798)
I'm not sure I follow your terse comment. Are you inferring that because skilled programmers have bugs in code with threads, that threads are bad? Isn't that a bit non-sequitur? Skilled programmers have bugs in thread with no threads! Should we therefore infer that if only they added threads, they would be bug free?

I am not arguing in favor (or against) threads. I am not contesting that they lead to more bugs or not. What I was contesting was the assertion was that "threads are not a natural fit." My point is that procedural programming (which nearly all programming is at least small level, minus languages like Prolog) is something human beings have been doing for thousands of years. Look at "recipe" for doing something, and you have procedural program. Look at any recipe for lots of people working together to do something, and you have a pool of cooperating procedures. It's something we get schooled in our whole lives.