News for hackers. Stuff that matters.
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?