Green threads
From Wikipedia, the free encyclopedia
In computer programming, Green threads are threads that are scheduled by a Virtual Machine (VM) instead of natively by the underlying operating system. Green threads emulate multithreaded environments without relying on any native OS capabilities. Green threads are managed in user space instead of kernel space, enabling them to work in environments that do not have native thread support.
Contents |
[edit] Performance
On a multi-core processor, native thread implementations can assign work to multiple processors, whereas green thread implementations cannot. In such an environment, native threads have a sizable advantage. On uniprocessor computers, however, the most efficient model has not yet been clearly determined. [1] Benchmarks on computers running the operating system GNU/Linux have shown that:
- green threads outperform Linux native threads on thread activation and synchronization.
- Linux native threads have much better performance on I/O and context switching operations.
Also a green thread may block all other threads if performing a blocking I/O operation. In order to avoid these problems, green threads must use asynchronous I/O operations, but the added complexity increases latency.
[edit] Green threads in Java virtual machine
Being created at the user space level, green threads are lighter than native threads, but as they implement a form of cooperative multitasking, bugs in threaded programs can bring the system to a halt.
In Java 1.1, green threads were the only threading model used by the JVM. As green threads have some limitations compared to native threads, subsequent Java versions dropped them in favor of native threads.
[edit] Green threads in other virtual machines
There are some other virtual machine languages that still implement equivalents of green threads instead of native threads. For example :
- Ruby [2] however, version 2.0 of Ruby does not implement green threads.
- MzScheme
- Haskell
- Smalltalk (most dialects: Squeak Smalltalk, VisualWorks, GNU Smalltalk, etc.)
The Erlang virtual machine has what might be called 'green processes' - they are like operating system processes (they do not share state like threads do) but are implemented within the Erlang Run Time System (erts). These are sometimes (erroneously) cited as 'green threads'.
In the case of GHC Haskell, a context switch occurs at the first allocation after a configurable timeout. GHC threads are also potentially run on one or more OS threads during their lifetime (there is a many-to-many relationship between GHC threads and OS threads), allowing for parallelism on symmetric multiprocessing machines, while not creating more costly OS threads than is necessary to run on the available number of cores.
The Smalltalk virtual machines do not count evaluation steps; however, the VM can still preempt the executing thread on external signals (such as expiring timers, or I/O becoming available). Certain Smalltalk implementations, e.g. QKS Smalltalk, do count evaluation steps, support green threads and prevent priority inversion. In most Smalltalk environments, a high-priority process that wakes up regularly will effectively implement time-sharing preemption:
[ [(Delay forMilliseconds: 50) wait] repeat ] forkAt: Processor highIOPriority
Most green thread implementations do not have any support for preventing priority inversion.