[OT] Stackless fibers/coroutines

Jacob Carlborg via Digitalmars-d digitalmars-d at puremagic.com
Tue Oct 4 11:59:15 PDT 2016


On 2016-09-26 16:31, Nick Sabalausky wrote:

> I don't know about async/await, because all my C# work was with versions
> of C# that predated those. But as for C#'s yield functions, yes, that's
> my understanding: It's hidden magic automatically inserted by the compiler.

This talk [1] from cppcon 2016, C++ Coroutines: Under the covers, gave 
some really nice insight how they implement coroutines in C++. It 
basically works like in Protothreads. The secret how the state is 
handled, I believe, is that all coroutines need to return a specific 
type that, I'm guessing, contains the state. Like generator<T> or 
task<T>. This return value is later used to resume the function. 
Directly calling the coroutine again will start a new invocation with a 
new state.

I'm guessing it works the same in C# [2]. The state is hidden in the 
IEnumerable that is returned, which will later be used in a foreach loop 
which will resume the coroutine.

[1] 
https://www.youtube.com/watch?v=bzAbe1VzhKk&index=51&list=PLHTh1InhhwT7J5jl4vAhO1WvGHUUFgUQH

[2] https://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list