Implement async/await using Fiber

Yuxuan Shui via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 19 19:14:59 PDT 2016


On Thursday, 19 May 2016 at 23:42:03 UTC, Ali Çehreli wrote:
> On 05/19/2016 12:57 PM, Yuxuan Shui wrote:
>>[...]
>
> You can use a delegate that takes a ref parameter:
>
> import std.stdio;
> import core.thread;
>
> void fiberFunc(ref string y) {
>     y = "produced_by_fiberFunc";
>     Fiber.yield();
> }
>
> void main() {
>     string data;
>     auto f = new Fiber(() => fiberFunc(data));
>     f.call();
>     writefln("Writing in main: %s", data);
> }
>
> Prints:
>
> Writing in main: produced_by_fiberFunc
>
> Also see std.concurrency.Generator. Both methods appear here:
>
>   http://ddili.org/ders/d.en/fibers.html
>
> Ali

Hmm... This could work. But I'm not satisfied with this solution. 
What if I have multiple yield sites in the fiber, and each have 
different return types?

Maybe I should use a Variant?


More information about the Digitalmars-d-learn mailing list