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:

    He hangs in shades the orange bright,
    Like golden lamps in a green night,
    And does in the pomegranates close
    Jewels more rich than Ormus shows;
    He makes the figs our mouths to meet,
    And throws the melons at our feet;
    But apples plants of such a price
    No tree could ever bear them twice.
    Andrew Marvell (1621–1678)

    Ye whose clay-cold heads and luke-warm hearts can argue down or mask your passions—tell me, what trespass is it that man should have them?... If nature has so wove her web of kindness, that some threads of love and desire are entangled with the piece—must the whole web be rent in drawing them out?
    Laurence Sterne (1713–1768)

    Wealth is so much the greatest good that Fortune has to bestow that in the Latin and English languages it has usurped her name.
    William Lamb Melbourne, 2nd Viscount (1779–1848)