18 ms·
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 event
by EGreg 9y 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.
- ahoka 9y agoCan you back up your claims?
- EGreg 9y agoYes, but I can't be bothered to. How's that for an honest response lol.
- amelius 9y ago> 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.
- littlestymaar 9y agoThis 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).
- jerf 9y agoAt 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).
- EGreg 9y agoReplace data structures with actors and messages and you got it :)
- littlestymaar 9y ago> 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-and-you-should-feel-bad/ http://www.jtolds.com/writing/2016/03/go-channels-are-bad-an...
- jerf 9y agoIf 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.)
- zzzcpan 9y agoEvent 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).
- notacoward 9y agoFew 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.
- EGreg 9y agoNo time to write it up.
- benjamin_mahler 9y agoKeep 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.
- chrisseaton 9y agoIn 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.
- deleted 9y ago[deleted]
- mamcx 9y agoHow 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(?))
- chrisseaton 9y agoIn 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.
- mamcx 9y agoAny code example? In this model I think deadlocks is possible, like in CSP. I wonder if CSP is closer to this model, right?
- maxxxxx 9y agoHow do you handle long lasting operations like checking something from time to time? Seems to me this is a natural use for a thread.
- winstonewert 9y agoSchedule a timer, and respond to the timer events. Its a waste of resources to have a thread hanging around sleeping all the time.
- jdmichal 9y agoThis is basically "There is no Thread": https://blog.stephencleary.com/2013/11/there-is-no-thread.html 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.
- pcwalton 9y agoGoroutines are just threads with a particularly idiosyncratic implementation.
- mjevans 9y agoEdit (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.
- computerex 9y agoThat's like saying chocolate cake is the best followed by blueberry muffins.