【发布时间】:2015-02-01 01:29:59
【问题描述】:
下面的这个短代码基于呼吸优先搜索。我希望将它与 OpenMP 并行,但没有成功。有没有办法让多个线程使用 OpenMP 并行结构将项目插入队列?
#include <omp.h>
#include <iostream>
#include <queue>
using namespace std;
int main(){
queue<int> q;
int v;
int cnt = 0;
for (int i =0; i<10; i++){
q.push(i);
cnt += 1;
}
while (!q.empty()) {
v = q.front();
q.pop();
cout << "v=" << v << ",";
#pragma omp parallel for
for (int i = 1; i < 6; i++) {
int j = v*i + 3;
if ( j < 13) {
q.push(j);
cnt += 1;
}
}
}
cout << endl << q.size() << endl;
cout << "count=" << cnt << endl;
return 0;
}
【问题讨论】:
-
std::queue::push 不是线程安全的,所以队列不可能
标签: c++ multithreading queue openmp