2

From wikipedia the paragraph Comparison with threads states:

... This means that coroutines provide concurrency but not parallelism ...

I understand that coroutine is lighter than thread, context switching is not involved, no critical sections so mutex is also not needed. What confuses me is that the way it works seems not to scale. According to wikipedia, coroutines provide concurrency, they work cooperatively. A program with coroutines still executes instructions sequentially, this is exactly the same as threads on a single core machine, but what about multicore machines? on which threads run in parallel, while coroutines work the same as on single core machines.

My question is how coroutines will perform better than threads on multicore machines?

4
  • They might or might not perform better, depending on circumstances. If there’s actual work to be done all the time they won’t be better than threads most of the time. If they’re dependent on each other and/or sequential anyway coroutines will be better. It’s quite broad subject without more specifics. Commented Apr 19, 2019 at 4:37
  • They don't perform better. Often implemented with a cycle-stealing threadpool today. Commented Apr 19, 2019 at 11:12
  • They have different goals. Coroutines are mostly purposed only for tasks load fairness, threads for both: balancing of a load and tasks load fairness so their are more universal than coroutines but for higher price (context switching, synchronization primitives). Commented Apr 19, 2019 at 12:07
  • Re, "coroutine is lighter than thread." I would not say "lighter." Coroutines and threads solve different problems. Commented Apr 19, 2019 at 13:16

1 Answer 1

3

...what about multicore machines?...

Coroutines are a model of concurrency (in which two or more stateful activities can be in-progress at the same time), but not a model of parallelism (in which the program would able to use more hardware resources than what a single, conventional CPU core can provide).

Threads can run independently of one another, and if your hardware supports it (i.e., if your machine has more than one core) then two or more threads can be performing their independent activities at the same instant in time.

But coroutines, by definition, are interdependent. A coroutine only runs when it is called by another coroutine, and the caller is suspended until the current coroutine calls it back. Only one coroutine from a set of coroutines can ever be actually running at any given instant in time.

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

3 Comments

Could you elaborate on in what scenarios coroutines will fit well as a solution and why?
I've never used coroutines in any real project. Seems like they would be useful when you've got two inter-related, stateful activities, and no clear reason why either one should drive the other. The example that I remember from school was a lexical analyzer and a parser. The top-level of the lexer was a loop that read characters from a source file, and "called" the parser each time it built a complete token. Meanwhile, the parser had a loop that "called" the lexer each time it wanted another token.
@neevek, They are often used for various UI tasks (as an example when it's needed to move plenty of objects on some game scene and using threads for this is impractical). Another very famous implementation from different area, is state machine behind the async/await mechanism in .NET where MoveNext method is used as a coroutine and is being called at different stages of an asynchronous flow execution.

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.