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?
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?
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