Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why green threads do not work on multiple cores

On wikipedia: Green_threads is described as normally cannot run on multi-cores without explaining why.

On a multi-core processor, native thread implementations can automatically assign work to multiple processors, whereas green thread implementations normally cannot.

I understand native threads can be assigned by OS to multi-cores. Can someone explain that why green threads can not run on multi-cores? Is it because green threads are derived/spawned from native threads, they cannot be moved from on native thread to another?

like image 926
Shengjie Avatar asked Jun 06 '13 15:06

Shengjie


People also ask

Can one thread run on multiple cores?

There is no such thing as a single thread running on multiple cores simultaneously. It doesn't mean, however, that instructions from one thread cannot be executed in parallel. There are mechanisms called instruction pipelining and out-of-order execution that allow it.

Why did Java remove green threads?

Original implementation: Green Threads In Java 1.1, green threads were the only threading model used by the Java virtual machine (JVM), at least on Solaris. As green threads have some limitations compared to native threads, subsequent Java versions dropped them in favor of native threads.

Can multiple cores run the same process?

Yes, a single process can run multiple threads on different cores. Caching is specific to the hardware. Many modern Intel processors have three layers of caching, where the last level cache is shared across cores.

How do green threads work?

Green Threads In software engineering, one alternative to native threads is green threads. This is where we are using threads, but they do not directly map to operating system threads. Instead, the underlying architecture manages the threads itself and manages how these map on to operating system threads.


2 Answers

The simple answer is: Wikipedia is wrong / inconsistent. Green threads can make use of M:N threading. Notably, this is how Erlang works. (I don't have a reference for this, but it shows up in most discussions of the VM.)

If you were being a language lawyer, then you could say that this doesn't happen "automatically". The green thread implementation has to take care of scheduling green threads on multiple native threads, instead of the operating system doing this implicitly. That said, the greater point remains, which is that it's very much possible for green threads to execute in parallel on a multicore system.

like image 99
millimoose Avatar answered Sep 20 '22 12:09

millimoose


I understand native threads can be assigned by OS to multi-cores. Can someone explain that why green threads can not run on multi-cores?

It is my understanding that one of the important goals of green threads is that they are managed completely by the software/VM without operating system intervention. It is the OS that helps "normal" threads fork the virtual processes and run them in parallel on multiple processors. The operating system sees multiple green-threads as a single thread to be scheduled on a single processor.

To quote from the wikipedia definition:

Green threads emulate multithreaded environments without relying on any native OS capabilities, and they are managed in user space instead of kernel space, enabling them to work in environments that do not have native thread support.

Running in a single processor has some important benefits to green-threads including no cached memory synchronization issues, faster startup, better overall synchronization performance. Most of these benefits are only possible if they are running in the same CPU.

Edit:

There have been lot of discussions about Erlang's and other languages use of multiple processors in their "green thread" implementations. I would argue that even if the word "green" is used by the language to describe these, they violate the classic definition. Certainly the terms are getting muddy but many are describing Erlang's threads as "green processes" to differentiate. They are definitely lightweight but the concepts and definition of "green threads" should not change even when there are overlapping but different implementations. I have yet to find the Erlang documentation describe their threading paradigm as "green".

Here's a number of pages that agree with this assessment:

  • Native Vs Green Threads
  • Four for the Ages
  • Green Vs. Native Threads
  • What is the difference between "green" threads and "native" threads?
like image 31
Gray Avatar answered Sep 24 '22 12:09

Gray