5

User-level threading involves N user level threads that run on a single kernel thread. What are the details of the user-level threading and how does this differ from coroutines?

2 Answers 2

2

Wikipedia has a quite in-depth summary on the subject: Thread (computing).

With Green threads there's a VM executing instructions that typically decides between to switch thread in-between two instructions.

With coroutines the two functions yield to each other at specified points, possibly passing values along, and typically requiring special language support. E.g. a producer yielding to a consumer, passing along an item.

Sign up to request clarification or add additional context in comments.

Comments

1

The idea behind user-level threads is to have multiple different logical threads running in the same program but to have the user program handle the mapping from logical threads to kernel threads (Which actually get scheduled) rather than having the OS handle the entire mapping. This can improve performance by letting the user program handle scheduling. Conceptually, user threads are one implementation of preemptive multitasking, where multiple jobs are run to completion in parallel by having the threads periodically stopped while other threads run.

Coroutines, on the other hand, are a generalization of standard function call and return ("subroutines") where functions pass control back and forth to one another, communicating values as they switch between routines. The switching back and forth between coroutines is under the control of the coroutines themselves; control only passes from one coroutine to another if one of the coroutines explicitly yields a value to another. This is an example of cooperative multitasking, where multiple jobs are completed in parallel by having the individual steps in the task manually coordinate who gets to run and when.

Hope this helps!

11 Comments

The way you describe coroutines yielding to other coroutines, how is that different from the way user-level threads yield?
@user782220- Coroutines continue to execute until they explicitly execute a yield statement to pass control to another coroutine. When they yield, they usually communicate a value to the coroutine they yield to, and they explicitly specify which corutine should execute next. Threads can be suspended at any time and with no notice, don't communicate values while yielding, and don't explicitly control which thread executes next.
There is something I'm missing. User-level threads are backed by one kernel thread and coroutines are backed by one kernel thread as well. When the backing kernel thread is suspended at any time for user-level threads how is that any different than the backing kernel thread of the coroutines being suspended.
@user782220- The difference is in what happens next. If you have a program with user threads, when one thread in that program is pulled off the processor, another one might get started up in the same program because your computation is, logically, a collection of independent threads coordinating together. With coroutines, when a kernel thread is paused, the next thread to run is probably not going to be a thread from the same program because, when using coroutines, the computation is not structured as a collection of threads all working in parallel.
@user782220- I think you should look up coroutines in more depth. While I'm glad to help, I feel like I'm giving you a complete introduction to coroutines and that there are much better resources for this online.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.