2

In C/C++, coroutines are implemented with stack-exchange hack, so stack-size is usually limited, doesn't grow automatically.

Does D Fiber has these limitations? Or does it grow automatically?

eonil
  • 83,476
  • 81
  • 317
  • 516
  • I have never used D but simply reading the manual shows [the constructor](http://dlang.org/phobos/core_thread.html#.Fiber) takes a parameter `size_t sz` which is *the stack size for this fiber*. So create a fiber with an artificially small stack size and try to blow it. – ta.speot.is Nov 09 '13 at 03:57
  • @ta.speot.is OK I'll try it. – eonil Nov 09 '13 at 04:05

1 Answers1

1

I tried with 4K initial fiber size, and D Fiber crashed if first stack overflows 4K. Anyway, after once yielded, it kept over 8K stack array variable in subroutines. So it seems grow the stack for each time yields. So it's not simply safe, programmer need to care stack size.

In addition to, D crashed for any printf regardless of size of stack… I don't know why… Here's my test code.

import std.stdio;
import std.concurrency;
import core.thread;
import std.container;
import std.conv;

void main()
{    
    Fiber[] fs = new Fiber[10];
    foreach (int i; 0..cast(int)fs.length)
    {
        fs[i] = new F1(i);
    };
    foreach (ref Fiber f ; fs)
    {
        f.call();
    };
    foreach (ref Fiber f ; fs)
    {
        f.call();
    };
    foreach (ref Fiber f ; fs)
    {
        auto s = f.state;
        writeln(s);
    };
}


class F1 : Fiber
{
    this(int idx)
    {
        super(&run, 4096);
        _idx = idx;
    }
private:
    int _idx;
    void run()
    {
        byte[3700] test1;
        //byte[1024] test2;
        writeln(_idx);
        //t1();
        this.yield();
        t1();
        //byte[1024] test3;
        //byte[1024] test4;
        writeln(_idx);
    }
    void t1()
    {
        byte[4096] test;
        //printf("T1!");
        t2();
    }
    void t2()
    {
        byte[2048] test;
        //printf("T2!");
        //t3();
    }
    void t3()
    {
        byte[2048] test;
        printf("T3!");
    }
}

Current result.

0
1
2
3
4
5
6
7
8
9
0
./build.bash: line 11: 26460 Segmentation fault: 11  ./tester
eonil
  • 83,476
  • 81
  • 317
  • 516