Green threads

From HandWiki
Short description: Lightweight threading implemented in userspace

In computer programming, green threads or virtual threads[disputed ] are threads that are scheduled by a runtime library or virtual machine (VM) instead of natively by the underlying operating system (OS). Green threads emulate multithreaded environments without relying on any native OS abilities, and they are managed in user space instead of kernel space, enabling them to work in environments that do not have native thread support.[1]

Etymology

Green threads refers to the name of the original thread library for the programming language Java (that was released in version 1.1 and then Green threads were abandoned in version 1.3 to native threads). It was designed by The Green Team at Sun Microsystems.[2]

History

Green threads were briefly available in Java between 1997 and 2000

Green threads share a single operating system thread through co-operative concurrency and can therefore not achieve parallelism performance gains like operating system threads. The main benefit of coroutines and green threads is ease of implementation

Performance

On a multi-core processor, native thread implementations can automatically assign work to multiple processors, whereas green thread implementations normally cannot.[1][3] Green threads can be started much faster on some VMs. On uniprocessor computers, however, the most efficient model has not yet been clearly determined.

Benchmarks on computers running the Linux kernel version 2.2 (released in 1999) have shown that:[4]

  • Green threads significantly outperform Linux native threads on thread activation and synchronization.
  • Linux native threads have slightly better performance on input/output (I/O) and context switching operations.

When a green thread executes a blocking system call, not only is that thread blocked, but all of the threads within the process are blocked.[5] To avoid that problem, green threads must use asynchronous I/O operations, although the increased complexity on the user side can be reduced if the virtual machine implementing the green threads spawns specific I/O processes (hidden to the user) for each I/O operation.[citation needed]

There are also mechanisms which allow use of native threads and reduce the overhead of thread activation and synchronization:

  • Thread pools reduce the cost of spawning a new thread by reusing a limited number of threads.[6]
  • Languages which use virtual machines and native threads can use escape analysis to avoid synchronizing blocks of code when unneeded.[7]

Green threads in the Java virtual machine

Original implementation: Green Threads

In Java 1.1, green threads were the only threading model used by the Java virtual machine (JVM),[8] at least on Solaris. As green threads have some limitations compared to native threads, subsequent Java versions dropped them in favor of native threads.[9][10]

An exception to this is the Squawk virtual machine, which is a mixture between an operating system for low-power devices and a Java virtual machine. It uses green threads to minimize the use of native code, and to support migrating its isolates.

Kilim[11][12] and Quasar[13][14] are open-source projects which implement green threads on later versions of the JVM by modifying the Java bytecode produced by the Java compiler (Quasar also supports Kotlin and Clojure).

Green threads in other languages

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

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 termed green threads, but have significant differences[clarification needed] from standard green threads.[citation needed]

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 needed to run on the available number of cores.[citation needed]

Occam is unusual in this list because its original implementation was made for the Transputer, and hence no virtual machine was needed. Later ports to other processors have introduced a virtual machine modeled 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:

[
    [(Delay forMilliseconds: 50) wait] repeat
 ] forkAt: Processor highIOPriority

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

See also

References

  1. 1.0 1.1 Sintes, Tony (2001-04-13). "Four for the ages". https://www.infoworld.com/article/2077383/four-for-the-ages.html. Retrieved 2020-07-14. "Green threads, the threads provided by the JVM, run at the user level, meaning that the JVM creates and schedules the threads itself. Therefore, the operating system kernel doesn't create or schedule them. Instead, the underlying OS sees the JVM only as one thread. Green threads prove inefficient for a number of reasons. Foremost, green threads cannot take advantage of a multiprocessor system(...) Thus, the JVM threads are bound to run within that single JVM thread that runs inside a single processor." 
  2. "Java Technology: The Early Years". 2014-12-22. http://java.sun.com/features/1998/05/birthday.html. 
  3. "What is the difference between "green" threads and "native" threads?". 2000-09-06. http://www.jguru.com/faq/view.jsp?EID=143462. "On multi-CPU machines, native threads can run more than one thread simultaneously by assigning different threads to different CPUs. Green threads run on only one CPU." 
  4. Comparative performance evaluation of Java threads for embedded applications: Linux Thread vs. Green Thread. 
  5. Stallings, William (2008). Operating Systems, Internal and Design Principles. New Jersey: Prentice Hall. pp. 171. ISBN 9780136006329. 
  6. Sieger, Nick (2011-07-22). "Concurrency in JRuby". Engine Yard. https://blog.engineyard.com/2011/concurrency-in-jruby. "For systems with large volumes of email, this naive approach may not work well. Native threads carry a bigger initialization cost and memory overhead than green threads, so JRuby normally cannot support more than about 10,000 threads. To work around this, we can use a thread pool." 
  7. Goetz, Brian (2005-10-18). "Java theory and practice: Synchronization optimizations in Mustang". IBM. http://www-128.ibm.com/developerworks/java/library/j-jtp10185/. 
  8. "Java Threads in the Solaris Environment – Earlier Releases". Oracle Corporation. http://docs.oracle.com/cd/E19455-01/806-3461/6jck06gqe/. "As a result, several problems arose: Java applications could not interoperate with existing MT applications in the Solaris environment, Java threads could not run in parallel on multiprocessors, An MT Java application could not harness true OS concurrency for faster applications on either uniprocessors or multiprocessors. To substantially increase application performance, the green threads library was replaced with native Solaris threads for Java on the Solaris 2.6 platform; this is carried forward on the Solaris 7 and Solaris 8 platforms." 
  9. "Threads: Green or Native". SCO Group. http://www.sco.com/developers/java/j2sdk122-001/ReleaseNotes.html#THREADS. "The performance benefit from using native threads on an MP machine can be dramatic. For example, using an artificial benchmark where Java threads are doing processing independent of each other, there can be a three-fold overall speed improvement on a 4-CPU MP machine." 
  10. "Threads: Green or Native". codestyle.org. http://www.codestyle.org/java/faq-Threads.shtml#greenthread. "There is a significant processing overhead for the JVM to keep track of thread states and swap between them, so green thread mode has been deprecated and removed from more recent Java implementations." 
  11. "kilim". https://github.com/kilim/kilim. 
  12. "Kilim". http://www.malhar.net/sriram/kilim/. 
  13. "Quasar Code on GitHub". https://github.com/puniverse/quasar. 
  14. "Parallel Universe". https://paralleluniverse.co/quasar. 
  15. "Chicken Scheme". http://wiki.call-cc.org/man/4/Unit%20srfi-18/. 
  16. "thezerobit/green-threads". https://github.com/thezerobit/green-threads. 
  17. "Application-level Stackless features – PyPy 4.0.0 documentation". http://doc.pypy.org/en/latest/stackless.html. 
  18. "Concurrency: GitBook". https://crystal-lang.org/docs/guides/concurrency.html. 
  19. "Fibers - Dlang Tour". https://tour.dlang.org/tour/en/multithreading/fibers. 
  20. "Threads: Overview". http://help.dyalog.com/latest/Content/Language/Introduction/Threads/Multithreading%20Overview.htm. ""A thread is a strand of execution in the APL workspace."" 
  21. @joeerl (23 June 2018). "Erlang processes are emulated in the Erlang VM, like Green threads - we like them since this simplifies many proble…". https://twitter.com/joeerl/status/1010485913393254401. 
  22. 22.0 22.1 "Go and Dogma". https://research.swtch.com/dogma. ""for example both Go and Haskell need some kind of “green threads”, so there are more shared runtime challenges than you might expect."" 
  23. "The Limbo Programming Language". http://www.vitanuova.com/inferno/papers/limbo.html. 
  24. "Multithreading in the MRI Ruby Interpreter ~ BugFactory". https://bugfactory.io/2014/10/10/multithreading-in-the-mri-ruby-interpreter/. 
  25. "Racket Places". http://docs.racket-lang.org/reference/places.html. "Places enable the development of parallel programs that take advantage of machines with multiple processors, cores, or hardware threads. A place is a parallel task that is effectively a separate instance of the Racket virtual machine." 
  26. "Using Threads to Run Code Simultaneously - The Rust Programming Language". https://doc.rust-lang.org/book/ch16-01-threads.html. 
  27. "Stackless.com: About Stackless". http://zope.stackless.com/about/sdocument_view. "A round robin scheduler is built in. It can be used to schedule tasklets either cooperatively or preemptively." 
  28. "Tcl event loop". http://wiki.tcl.tk/1527. 

External links