5 ms·
> The erlang BEAM VM is the only system designed to do concurrency correctly. That's a fairly bold claim. Erlang is good at the actor model. It's not an all-ar
by wyager 12y ago
> The erlang BEAM VM is the only system designed to do concurrency correctly.
That's a fairly bold claim. Erlang is good at the actor model. It's not an all-around winner in concurrency.
Another shining example is the Glasgow Haskell Compiler runtime system. It has an exceptionally fast and efficient green thread system that (in the latest version of GHC) can scale almost-linearly up to 32 cores, even with some resource contention, and can easily handle millions of concurrent threads. I suspect you could emulate Erlang actors with very little loss of efficiency. (And, of course, you're also free to use a thread-based model.)
And being a language with type-system-encapsulated mutability (and very nice resource contention management mechanisms like STM), concurrency is often "free" and very safe. An example of how concurrency is "free":
main = do
forkIO (putStrLn "thing1")
forkIO (putStrLn "thing2")
threadDelay 1000
Anything that is IO typed can get free concurrency like this. I need make no change to my putStrLn code: as long as it returns IO, I can use it concurrently.
A few interesting things to look at:
http://benchmarksgame.alioth.debian.org/u32/performance.php?test=threadring http://benchmarksgame.alioth.debian.org/u32/performance.php?...
http://haskell.cs.yale.edu/wp-content/uploads/2013/08/hask035-voellmy.pdf http://haskell.cs.yale.edu/wp-content/uploads/2013/08/hask03...
edit: Added thread delay so code example works out of the box.
- millstone 12y agoI tried this code with ghc 7.6.3: import Control.Concurrent main = do forkIO (putStrLn "thing1") forkIO (putStrLn "thing2") It only outputs "thing1", not "thing2". What gives?
- wyager 12y agoThe whole process dies before "thing2" gets a chance to do anything. The idea is that if the main thread is done and not waiting, we don't really care about what any of the child threads are doing, so we just axe them.
- millstone 12y agoHmm, ok. I tried adding an infinite loop after the forkIO calls: import Control.Concurrent loop :: Int -> Int loop x = loop x main = do forkIO (putStrLn "thing1") forkIO (putStrLn "thing2") putStrLn $ show (loop 0) Now the program runs forever, but to my surprise doesn't output anything. I think Haskell's concurrency must be very different from the types I'm familiar with.
- dragonwriter 12y agoThe parallelism (or not) of GHC concurrency depends on various options; if you don't link with the "-threaded" option, you are running green threads in a single native thread (N:1 instead of M:N). See: http://www.haskell.org/haskellwiki/Concurrency http://www.haskell.org/haskellwiki/Concurrency and http://www.haskell.org/ghc/docs/7.0.1/html/users_guide/using-smp.html http://www.haskell.org/ghc/docs/7.0.1/html/users_guide/using... And Haskell threads aren't preempted except on memory allocation, so a tight loop without memory allocation never is preempted, so the main thread here never is forced out of control.
- wyager 12y ago>I think Haskell's concurrency must be very different from the types I'm familiar with. It's very different from most concurrency systems. It's a very, very lightweight cooperative green thread system. We're kind of dealing with pathological edge cases here. We're forking to do exceptionally simple, fast things (you shouldn't do that) and we're trying to sleep by wasting CPU cycles (you shouldn't do that). For example, this works fine: import Control.Concurrent main = do forkIO (putStrLn "thing1") forkIO (putStrLn "thing2") threadDelay 1000 threadDelay doesn't hog CPU cycles, so the other threads are able to run.
- millstone 12y agoThank you for the explanation. Your code works for me too.
- kaoD 12y agoNot a user of Haskell but interested in it. What makes my spider-sense tingle in Haskell is lazyness. It's all cool until you unexpectedly hit that lazy unroll (not sure what the correct term is) in a time-critical system. Are there ways to control lazyness in Haskell, just like there are ways to control mutable state?
- kazagistar 12y agoOf course; careful control of evaluation strategies is one of the methods by which parallelism is accomplished. At the simpler level, parameters and items in records can be strictly evaluated. However, once you get used to it, you start to find that strict evaluation is rarely useful.
- wyager 12y agoLaziness is just the default (and most likely to terminate) evaluation strategy. You can use a number of techniques (the simplest being Bang Patterns) to enforce non-lazy evaluation strategies (including strict evaluation, parallel evaluation, sequential evaluation, etc.). However, there are very few activities where Haskell's laziness will cause disruptive timing variations. Any language that you might use instead of Haskell is probably heavily heap-oriented, and likely garbage collected, which means that you're already dealing with unpredictable timing variance.
- dragonwriter 12y ago> Are there ways to control lazyness in Haskell Yes. http://www.haskell.org/haskellwiki/Performance/Strictness http://www.haskell.org/haskellwiki/Performance/Strictness
- rdtsc 12y ago> That's a fairly bold claim. I don't know seem true and practical to me. The industry (WhatsApp, Riak, CouchDB, RabbitMQ) and chances are, even your smart phones disagree with you. They are many other closed source applications in the industry that reliably handle concurrency using Erlang. > I suspect you could emulate Erlang actors No you can't unless you run in an isolated heap and add a many years battle tested VM system and libraries built for concurrency and fault tolerance. Otherwise yes, anyone can make a thread work with thread-safe queue -- look Erlang! Nope. Not Erlang. It is a fork-join or just a plain thread+queue. > It's not an all-around winner in concurrency. Yes it is. The winner in concurrency is system that has most fault tolerance tooling built in. That is true secret. "Millions of concurrent" threads are useless if the crash of one brings down the rest.
- wyager 12y ago>unless you run in an isolated heap Yes, Haskell already has per-OS-thread heaps, and there's not really any need for isolation since everything is immutable and GCed anyway. >many years battle tested VM system The VM is an implementation detail, not a feature. But yes, the GHC RTS is basically a VM. >libraries built for concurrency and fault tolerance Yes, Haskell has those. Specifically, libraries work no matter how they're being executed (concurrently or otherwise), and they're fault tolerant in the sense that error conditions are encapsulated in the type system and the libraries do not generally crash. >It is a fork-join or just a plain thread+queue. Well, not necessarily. I can express an actor however I want. Haskell forks aren't like regular C forks; they're much more flexible. It's basically the same thing as Actors in Erlang. The thread only gets picked up when something happens to a resource it's waiting on (like actors in Erlang). >The winner in concurrency is system that has most fault tolerance tooling built in. Well, Haskell has excellent fault tolerance support, but the recommended programming style is just to never crash, which is an entirely reasonable request when you're writing in Haskell. >"Millions of concurrent" threads are useless if the crash of one brings down the rest. There's no particular reason it would. See the `catch`, `handle`, etc. combinators for how to deal with errors in Haskell (although again, it's recommended just to write programs that don't crash).