News for hackers. Stuff that matters.
Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by EGreg on Tuesday June 13, 2017 @02:06PM (#14547081)
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.
Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by littlestymaar on Tuesday June 13, 2017 @03:37PM (#14547890)
This presentation is not about Kernel thread versus green thread, it's about threads vs events. In this regards, Goroutines are as bad as threads since it requires some synchronization witchcraft (channel or locks) and the author advocates for your «second best one» (events).
Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by jerf on Tuesday June 13, 2017 @04:52PM (#14548541)
At the time that this was written, "threading" encompassed the idea of threads sharing and mutating basically all of their state. In practice not every thread would touch everything, of course, but the concept model was that it was at least possible, and that it was the responsibility of data structures to be ready for concurrent mutation.

While Go in theory is the same as a 1990s-style-threading model, and serious Go programmers had best not forget that in the final analysis they aren't actually protected from multithreading problems simply "because Go", having a model where structures are owned by particular threads, and things are shared via messages rather than direct threads, thus making it the thread's responsibility to be ready for incoming messages, makes it vastly, vastly easier to program in in practice.

Yes, this absolutely could have been adapted back then, too. This once again goes back to the fact that a language is often more than just the language itself, but also includes the standard idioms and the standard and common libraries. You could have written your code in 1990s in the Go way in C++, but you would have had to bring up basically an entirely new library stack and written a lot of wrapping code around OS functionality.

Despite the fact that Go programming is in practice much simpler than the threading being criticized in that paper, I agree strongly with the idea that Go would be enhanced by the ability to erect much stronger boundaries around goroutines to ensure that they neither share nor reach into anything they shouldn't. Still, there are ones by convention, which is way better than nothing.

You should also consider Rust-style "synchronization witchcraft" as a very interesting alternative "isolated actors" and "locks everywhere" that strikes out in a new direction entirely. In 50 years what we currently consider all of our best sync options may in hindsight have proved to been simply a false dichotomy (albeit one with three or four choices rather than just 2, but, the same principle holds).

Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by littlestymaar on Tuesday June 13, 2017 @05:55PM (#14549001)
> and things are shared via messages rather than direct threads, thus making it the thread's responsibility to be ready for incoming messages, makes it vastly, vastly easier to program in in practice.

I kind of disagree, the Go mantra «don't communicate by sharing memory, share memory by communicating» is merely a marketing pitch, especially due to the clumsy nature of Go's channel[1].

Unfortunately, the number of Go developers being bitten by data races in Go is too high … The main danger here is the illusion of safety that the Go community is spreading (and your comment is an example of that): all the Go devs I know (including me) never really worked on multi-threaded C or C++ code before, and if you come from a nodejs or Python background like most do, you aren't really prepared to understand the challenges of multi-threaded programs. Sometimes the best way to structure your program is just to have a shared `map` of items you need to process, but if you forgot to wrap it in a mutex, you have a data-race. And if you don't even know what a data-race is, you face strange and unpredictable bugs (true story).

[1] : http://www.jtolds.com/writing/2016/03/go-channels-are-bad-an...

Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by jerf on Tuesday June 13, 2017 @08:35PM (#14549912)
If you have a public map that is being accessed by multiple goroutines, you've fought Go not once but twice; once for the multiple access, and once for it being public in the first place. At the very least make the map private and create an async-safe API around it.

You definitely do have some responsibilities learning how to write truly multithreaded code after coming out of a single-threaded environment. However, having used quite a few environments, I don't know of any where that isn't true. You've fundamentally stepped up a level of complexity. In Haskell, you don't have to change too much about your style, excepting that you first had to write your code in Haskell, which most people consider quite a leap. In Erlang, you are forced to deal with process boundaries and there isn't really an equivalent of "just share a map"; even the obvious "just share a map" option (ets) is simply a prepared API around a process boundary. Rust's pretty cool, but you must write in accordance with its type system, and that's going to fundamentally change how you write programs if it's your foray into that sort of world, which is cool and good, but far more disruptive than learning how to write some Go.

If Go makes a particular error here, it is that it doesn't stick the problems in your face and makes it a bit too easy. I'm actually on the side of that being a problem (if you think I'm all "rah rah" Go, go back and read the grandparent post with an eye for counterexamples, there's at least three), but I think most people coming from Python or Node would at least initially believe they prefer this.

The reason I am a bit rah-rah on Go (just not unreservedly) is that while I still if I had my way would tweak some important aspects about it is that it's the first time it has brought that style of thinking (and libraries!) to the Algol-style languages. On the one hand, as a language dilettante I'm saddened that we lose some goodness from the non-Algol heritages, but on the other hand, it means I get to use it for my job without cringing. Haskell is... Haskell. Erlang's a neat language but it conflated a lot of stuff and forces a lot of adaptations on to you that I consider unnecessary. (I respect it and its developers deeply, but as is often the case the first effort in a certain space often makes errors that later generations will consider basic.) The C/C++/Java/C# space all pretty much went with 1990s-style threading. It's probably still the easiest entry into that space you can have, so I have a hard time laying too much blame at the language's feet rather than the problem domain's feet. (The channel criticisms are mostly legit, but at the same time, Go channels are probably better primitives than 80% of programmers have ever had access to. To see the complete picture one most manage to hold all of these aspects in one's head at once.)

Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by EGreg on Tuesday June 13, 2017 @05:51PM (#14548978)
Replace data structures with actors and messages and you got it :)
Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by zzzcpan on Tuesday June 13, 2017 @05:13PM (#14548694)
Event driven programming is actually very close to actor model conceptually, just slightly less powerful. And basically switching to actor model is the only path forward for someone coming from events.

But yeah, people need to stop confusing actor model with shared-memory multithreading. Threads/green-thread/goroutines are all the same and the criticism applies to all of them. They share nothing with actor model (pun intended).

Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by pcwalton on Tuesday June 13, 2017 @05:05PM (#14548643)
Goroutines are just threads with a particularly idiosyncratic implementation.
Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by mjevans on Wednesday June 14, 2017 @02:51AM (#14551066)
Edit (first line); actually maybe it's my definition of 'thread' that is different. I think of a process as a security context that has one or more CPU routines (complete set of state). Goroutines are not threads in that sense, they lack that CPU state and are just segments of code (logic and data-state).

Goroutines are 'soft threads'. The underlying implementation typically defaults to number of CPU cores as max possible REAL thread count. The real threads will check the message queues for possible wakeups when the previous soft thread blocks or terminates.

Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by chrisseaton on Tuesday June 13, 2017 @03:44PM (#14547960)
In terms of safety I think fork-join is better than the actor model. The actor model is all about mutable state - the state of all the actors. Fork-join removes state entirely - it's just about passing values.
Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by dragonwriter on Tuesday June 13, 2017 @11:18PM (#14550463)
> The actor model is all about mutable state

One of the more popular actor model languages (Erlang) also generally avoids mutable state (it's available in the form of the process dictionary, but recognized best practice is to avoid using it.)

Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by chrisseaton on Wednesday June 14, 2017 @02:22AM (#14550960)
That's not quite what I mean.

I mean an Erlang actor is a state machine isn't it? It accepts messages, and each messages causes the actor to either return some data based on its state, or to enter a new state.

State machine - so it's holding state isn't it? And it's mutable because you can cause it to enter a new state by sending messages.

And you have lots of these mutable state machines in your Erlang process - lots of mutable state.

See what I mean? I know they don't have shared memory, but each process is effectively a little piece of global shared mutable state. And that's bad!

In other parallelism models like fork-join, there is no mutable state. Tasks get input parameters, and produce a result. They don't have any state in any meaningful sense because you can't observe their state externally and you can't ask them to change state externally.

Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by mamcx on Tuesday June 13, 2017 @04:42PM (#14548447)
How is the fork-join? How a program made in this model is better? I don't remember see any concrete example with that (I see CSP, Actor and Disruptor(?))
Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by chrisseaton on Tuesday June 13, 2017 @04:46PM (#14548481)
In the actor model you can have non-determinism. Two actors are going to send you messages to update the state in a third. Depending on which message arrives first, you can arrive at a different final state in the third actor. And which can arrive first depends on scheduling issues that can change between runs. That's non-deterministic, and it's part of what makes multi-threading so hard.

The fork-join model doesn't have this problem because you can only join a determinate number of tasks, and you have to join them in a determinate order. You can't have anything arriving at any other order than you specified. Scheduling issues cannot change how a fork-join program runs.

In the actor model you also have a really complicated state situation. You've got n actors, all each in a possibly infinite number of states. Because of the non-determinism described above the states your actors will get into in each run of the program can be different. That's another part of what makes multi-threading so hard.

The fork-join model doesn't have this problem because tasks don't have any state. They just have input and output. There's no state, only commutation.

The actor model is a big uncontrolled graph of state in my opinion. Fork-join is DAG of pure functional computation done in parallel.

Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by mamcx on Tuesday June 13, 2017 @04:56PM (#14548575)
Any code example?

In this model I think deadlocks is possible, like in CSP. I wonder if CSP is closer to this model, right?

Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by chrisseaton on Tuesday June 13, 2017 @05:04PM (#14548635)
No CSP is all about mutable state in each process.

Fork-join looks like, where we blur an image with two-way parallelism:

   a, b = divide image into halves
   fork {
     a' = blur a
     b' = blur b
   }
   result = combine halves a' b'
So all the tasks in the fork { } block run in parallel. The task that created those tasks stops and waits for them to all finish and to have all their results before it continues.

And there's no way the order of the tasks finishing can change the result because you always have to wait for all your tasks to finish.

See how there's no mutable state? You can only start a task, and accept its result. You can't see or change anything in between that.

You can get a deadlock if one task crashes. Some people really care about that, some people see it as an implementation detail and wouldn't really define it as a 'deadlock' in the same way.

Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by kuschku on Tuesday June 13, 2017 @06:25PM (#14549185)
So, you basically have what JS calls await Promise.all(a, b) ?
Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by chrisseaton on Tuesday June 13, 2017 @06:34PM (#14549250)
Yes you can build fork-join that way, but you'd need to remember to manually follow some rules. You should create the tasks a and b immediately before you await, and do no other work between creating them and awaiting. And you should not cause any side effects in the two tasks.

Systems which have fork-join built-in would either help you to follow those rules, or force you to.

Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by mamcx on Tuesday June 13, 2017 @10:35PM (#14550325)
So, if I understand, you "tasks" are almost pure functions:

   fork -> send -> task1(input) & task2(input) -> joiner
And you must always(?) have a join? so I can't have a zombie task elsewhere?

I probably like how this sound! Exist a more detailed material to study on this?

Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by notacoward on Tuesday June 13, 2017 @03:44PM (#14547953)
Few would disagree with "better" (than either pure threads or pure events) but if you're going to say "best" the burden of proof is higher. Please explain why you think actors are strictly better than e.g. promises/futures or agent-based models.
Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by benjamin_mahler on Wednesday June 14, 2017 @01:33AM (#14550835)
Keep in mind that actors and futures can be used together, which produces an even more powerful result than either abstraction used alone. We use this programming model in Mesos via a library called libprocess. There will be some talks on this approach in upcoming MesosCons and we'll submit one to Cppcon.
Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by EGreg on Tuesday June 13, 2017 @04:19PM (#14548249)
No time to write it up.
Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by amelius on Tuesday June 13, 2017 @03:29PM (#14547836)
> The actor model where actors can be scheduled on any thread is the best (Erlang, Goroutines).

Yes, but only if there can be structural sharing between threads.

Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by maxxxxx on Tuesday June 13, 2017 @04:05PM (#14548122)
How do you handle long lasting operations like checking something from time to time? Seems to me this is a natural use for a thread.
Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by winstonewert on Tuesday June 13, 2017 @04:10PM (#14548168)
Schedule a timer, and respond to the timer events. Its a waste of resources to have a thread hanging around sleeping all the time.
Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by jdmichal on Tuesday June 13, 2017 @06:06PM (#14549070)
This is basically "There is no Thread":

https://blog.stephencleary.com/2013/11/there-is-no-thread.ht...

There are times when you can offload an asynchronous task to non-CPU hardware, in which case the blocking thread can disappear. The thread reappears when the hardware asynchronously informs the CPU of task completion. (This idea is what Node.JS is entirely reliant upon.)

Consider if we didn't have timing hardware to handle this task for us. We could implement a timer with a spin-loop thread which checks the clock and fires off events.

Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by computerex on Tuesday June 13, 2017 @05:21PM (#14548759)
That's like saying chocolate cake is the best followed by blueberry muffins.
Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by ahoka on Tuesday June 13, 2017 @03:28PM (#14547829)
Can you back up your claims?
Re:Why Threads are a Bad Idea (for most purposes) (1995) [pdf] by EGreg on Tuesday June 13, 2017 @03:33PM (#14547862)
Yes, but I can't be bothered to.

How's that for an honest response lol.