Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Scala and Erlang use green threads?

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 processor? 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?

like image 992
CHAPa Avatar asked Apr 02 '10 13:04

CHAPa


2 Answers

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
like image 130
Christian Avatar answered Oct 12 '22 19:10

Christian


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.

like image 43
Rex Kerr Avatar answered Oct 12 '22 19:10

Rex Kerr