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:

    Many a green isle needs must be
    In the deep wide sea of Misery,
    Or the mariner, worn and wan,
    Never thus could voyage on
    Percy Bysshe Shelley (1792–1822)

    How have I been able to live so long outside Nature without identifying myself with it? Everything lives, moves, everything corresponds; the magnetic rays, emanating either from myself or from others, cross the limitless chain of created things unimpeded; it is a transparent network that covers the world, and its slender threads communicate themselves by degrees to the planets and stars. Captive now upon earth, I commune with the chorus of the stars who share in my joys and sorrows.
    Gérard De Nerval (1808–1855)

    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)