我有一段时间试图让 D 中的屏障同步正常工作。我目前没有收到任何编译器错误,但是每次到达障碍时我都会遇到分段错误。这基本上是我所拥有的:
import std.stdio;
import std.conv;
import std.concurrency;
import core.thread;
import core.sync.barrier;
//create barrier
Barrier barrier;
void the_thread()
{
barrier.wait(); //I get a segmentation fault here
}
void main(string[] args)
{
int threads = to!int(args[1]); //number of threads
//init barrier
barrier = new Barrier(threads);
//launch threads
foreach(i; 0 .. threads)
{
spawn(&the_thread);
}
thread_joinAll();
}
我已经尝试在主函数中完全定义屏障,但 dmd 抱怨:
static assert "Aliases to mutable thread-local data not allowed."
我也尝试将它作为共享变量传递,我得到了这个:
non-shared method core.sync.barrier.Barrier.wait is not callable using a shared object