在C/C++中,协程是用栈交换hack实现的,所以栈的大小通常是有限的,不会自动增长。
D光纤有这些限制吗?或者它会自动增长?
发布于 2013-11-09 12:38:54
我尝试了4K的初始纤程大小,如果第一个堆栈溢出4K,D纤程就会崩溃。无论如何,一旦产生后,它在子程序中保存了超过8K的堆栈数组变量。因此,似乎每次都会增加堆栈的收益率。所以它不是简单的安全,程序员需要关心堆栈的大小。
此外,无论堆栈…的大小如何,对于任何printf,D都会崩溃我不知道为什么…这是我的测试代码。
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!");
}
}当前结果。
0
1
2
3
4
5
6
7
8
9
0
./build.bash: line 11: 26460 Segmentation fault: 11 ./testerhttps://stackoverflow.com/questions/19872149
复制相似问题