【发布时间】:2017-04-03 22:58:15
【问题描述】:
对于以下带有 openMP 的多线程程序,我可以做些什么来防止其他线程在一个线程写入“stuff”时读取“stuff”向量?
vector<int> stuff; //Global vector
void loop() {
#pragma omp parallel for
for(int i=0; i < 8000; i++){
func(i);
}
}
void func(int& i) {
vector<int> local(stuff.begin() + i, stuff.end()); //Reading and copying global vector "stuff"
//Random function calls here
#pragma omp critical
stuff.assign(otherstuff.begin(), otherstuff.end()); //Writing to global vector "stuff"
}
【问题讨论】:
-
请注意,引用的问题有更多惯用的 OpenMP 答案。
标签: c++ multithreading openmp