Green Threads - Green Threads in Other Languages

Green Threads in Other Languages

There are some other programming languages that still implement equivalents of green threads instead of native threads. Examples:

  • CPython with greenlet, eventlet and gevent
  • Go
  • Haskell
  • Limbo
  • Lua uses coroutines for concurrency. The reference implementation limits their use to Lua code, but the coco extension allows true coroutine semantics in C.
  • Occam, which prefers the term 'process' instead of 'thread' due to its origins in Communicating sequential processes
  • Ruby before version 1.9
  • Racket (native threads are also available through Places)
  • SML/NJ's implementation of Concurrent ML
  • Smalltalk (most dialects: Squeak, VisualWorks, GNU Smalltalk, etc.)
  • Stackless Python—supports either preemptive multitasking or cooperative multitasking through microthreads (so-called tasklets).

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 referred to as 'green threads', but have significant differences from standard 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.

Occam is unusual in this list because its original implementation was tied to the Transputer, and hence no virtual machine was necessary. Later ports to other processors have introduced a virtual machine modelled on the design of the Transputer, an effective choice because of the low overheads involved.

Most 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). Usually round-robin scheduling is used so that a high-priority process that wakes up regularly will effectively implement time-sharing preemption:

[ repeat ] forkAt: Processor highIOPriority

Other implementations, e.g. QKS Smalltalk, are always time-sharing. Unlike most green thread implementations, QKS Smalltalk also has support for preventing priority inversion.

Read more about this topic:  Green Threads

Famous quotes containing the words green, threads and/or languages:

    The birch begins to crack its outer sheath
    Of baby green and show the white beneath....
    Robert Frost (1874–1963)

    Experience is never limited, and it is never complete; it is an immense sensibility, a kind of huge spider-web of the finest silken threads suspended in the chamber of consciousness, and catching every air-borne particle in its tissue.
    Henry James (1843–1916)

    The trouble with foreign languages is, you have to think before your speak.
    Swedish proverb, trans. by Verne Moberg.