I've been reading a lot about how Scala and Erlang does lightweight threads and their concurrency model (actors).
However, I have my doubts.
Do Scala and Erlang use an approach similar to the old thread model used by Java (green threads) ?
For example, suppose that there is a machine with 2 cores, so the Scala/Erlang environment will fork one thread per proces开发者_如何学Csor? The other threads will be scheduled by user-space (Scala VM / Erlang VM ) environment. Is this correct?
Under the hood, how does this really work?
Erlang is using user-space multitasking, tasks run until they block or until they have used up their share of 'reductions'. A reduction is vaguely defined as a unit of computation.
Up until the SMP scheduler, there was just one kernel thread taking runnable tasks. With SMP scheduling you have several kernel threads taking tasks, and thus executing code in parallel on multi-core machines. The number of scheduler threads should match the number of cores. See the -smp [enable|auto|disable]
switch in the erl manpage.
There has also been a pool of kernel threads for loadable drivers to execute blocking system calls in. This is called the async thread pool. See +A size
in the erl manpage.
Further reading
- http://www.erlang.org/pipermail/erlang-questions/2008-September/038231.html
- http://erlang.org/euc/08/euc_smp.pdf
Scala 2.8 uses Java thread pools. The lightweight actors (Reactor
) and the heavier actors in lightweight mode (react {...}
) don't take their own thread; rather, when they have a message, they take a thread until they're done processing the message, then give back the thread and don't run at all until the next message hits.
This article gives a decent description of Actors in 2.7; 2.8 is not that different.
For recent information about Erlang implementation details check fresh talk (slides).
Scala uses the underlying Java thread implementation, which uses native threads.
Can't say about Erlang.
精彩评论