Yield from function?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 30 22:32:02 PST 2017


On 01/30/2017 08:12 PM, Profile Anaysis wrote:
>
> import std.stdio, std.concurrency, core.thread;
>
> class Search : Fiber
> {
>     this() { super(&start); }
>     int res = 0;
>     void start()
>     {
>     Fiber.yield();
>         res = 1;
>     }
> }
>
> void main()
> {
>
>     auto search = new Search();
>
>     search.call(); writeln(search.res);
>     search.call(); writeln(search.res);
>     search.call(); writeln(search.res); // crashes after 3rd call(first
> two work fine)
> }

That's because the fiber is not in a callable state. (You can check with 
search.state.) Here is one where the fiber function lives (too) long:

import std.stdio, std.concurrency, core.thread;

class Search : Fiber
{
     this() { super(&start); }
     int res = 0;
     void start() {
         while (true) {
             Fiber.yield();
             ++res;
         }
     }
}

void main()
{
     auto search = new Search();

     foreach (i; 0 .. 5) {
         search.call();
         writeln(search.res);
     }
}

Ali



More information about the Digitalmars-d-learn mailing list