【发布时间】:2016-07-13 19:46:12
【问题描述】:
我花了很长时间试图让 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
【问题讨论】:
-
将屏障标记为
__gshared使其对我有用(在 Windows 上)。
标签: synchronization d dmd barrier