我想知道如何利用我的核心来使循环并行运行。让我们说我想将一个大数组的所有成员初始化为1。 为了检查初始化是否正确完成,最后,我添加了数组的所有值,但是我得到了零。如何让线程共享数据?
这是我的尝试:
<td>
答案 0 :(得分:1)
这里复制数组:
public Worker (ref uint64[] arr, uint64 start, uint64 end, int val) {
this.arr = arr;
this.start = start;
this.end = end;
this.val = val;
}
作业正在此处创建副本。您可以使用unowned
变量来解决此问题:
class Worker {
// The array is not owned by the worker!
public unowned uint64[] arr;
public uint64 start;
public uint64 end;
public int val;
public Worker (ref uint64[] arr, uint64 start, uint64 end, int val) {
this.arr = arr;
this.start = start;
this.end = end;
this.val = val;
}
}