This short code in below is based on the breath first search. I would hope to parallel it with OpenMP but have no success. Is there any way to have multiple threads inserting items to queue using OpenMP parallel construct ?
#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;
}