什么时候在OMP中使用关键部分和障碍?

时间:2013-03-29 07:43:00

标签: c++ parallel-processing shared critical-section openmp

从每个线程读取公共向量/数组中的数据时,是否必须使用临界区?可能有任何冲突吗?

#include <omp.h>
#include <vector>
// ...

using namespace std;

// ...

int main(int argc, char** argv)
{
    vector<double> data;
    // populating vector from a file, for example...

    #pragma omp parallel num_threads(4)
    {
        // ...

        // in this cycle I only READ from the vector 'data'
        #pragma omp critical
        {
            for (vector<double>::iterator it = data.begin(); it < data.end(); it++)
            {
                double value = *it;
                // do some actions with 'value'...
            }
        }

        // ...
    }

    // ...

    return 0;
}

从普通(共享)向量读取时,我担心线程可能无法正常工作。我是对的吗?

0 个答案:

没有答案