Green Vs Native Threads

This page discusses the advantages and disadvantages of "green threads" (a term that AFAIK comes from JavaLanguage)--threads simulated by a language runtime, vs "native threads"--threads provided by the operating system.

Green threads:

NativeThreads?:

Notice that both types of threads are subject to the bugaboos one encounters when dealing with SharedStateConcurrency--deadlock, race conditions, etc. It's very common to write an application on a GreenThreads? platform and find that it fails when run on a NativeThreads? implementation (or vice versa). GreenThread? schedulers are generally more deterministic than NativeThread? schedulers, and there are many race conditions that only occur on native thread implementations. OTOH, the deterministic nature of GreenThreads? can also cause problems with apps expecting native threading semantics.

Careful implementations of GreenThreads? can be very lightweight. A good example is OzLanguage's built-in thread support, which effortlessly tracks thousands of threads to support an interesting style of constraint-solving programming. (Most of these threads are usually blocked, but the efficiency with which these numbers are managed is still impressive.)


How about PooledGreenThreads?? A (proposed) arrangement where the runtime keeps a small pool of kernel threads (typically equal to the number of CPUs, or a small multiple thereof), and assigns logical user threads to the physical threads as it sees fit (if the # of threads in the pool is 1, then this degenerates to green threads). Sufficient concurrency to take advantage of multiple CPUs. Coupled with asynchronous or multiplexed IO to handle the blocking issue, and this architecture could have many of the advantages of both green and native threads, without the drawbacks. Two remaining issues:


See also: CooperativeThreading CooperativeThreadingOperatingEnvironment MessagingAsAlternativeToMultiThreading GreenJavaThreads NativeJavaThreads ThreadingConsideredHarmful?


EditText of this page (last edited September 10, 2009) or FindPage with title or text search