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:

    “by Spoon Rivergathering many a shell,
    And many a flower and medicinal weed—
    Shouting to the wooded hills, singing to the green valleys.
    At ninety-six I had lived enough, that is all,
    And passed to a sweet repose.”
    —Edgar Lee Masters (1869–1950)

    “All is waste and worthless, till
    Arrives the selecting will,
    And, out of slime and chaos, Wit
    Draws the threads of fair and fit.”
    —Ralph Waldo Emerson (1803–1882)

    “The less sophisticated of my forbears avoided foreigners at all costs, for the very good reason that, in their circles, speaking in tongues was commonly a prelude to snake handling. The more tolerant among us regarded foreign languages as a kind of speech impediment that could be overcome by willpower.”
    —Barbara Ehrenreich (b. 1941)