6

以下代码应该非常简单,但在尝试对具有嵌套 OpenMP 代码的线程执行 .join() 时,似乎最终会陷入挂起状态。使用 GCC 编译器 4.7.2 x64 和来自http://sourceforge.net/projects/mingwbuilds的pthreadsg++ threadexample.cpp -Wall -std=c++11 -fopenmp -o threads

// threadexample.cpp
#include <iostream>
#include <thread>
#include <omp.h>

using namespace std;

void hello(int a) {

    #pragma omp parallel for
        for (int i=0;i<5;++i) {
            #pragma omp critical
            cout << "Hello from " << a << "! " << "OMP thread iter " << i << endl;
        }

    cout << "About to return from hello function" << endl;
}

int main (int argc, char ** argv) {

    thread t1(hello, 1); //fork
    cout << "t1 away!" << endl;
    thread t2(hello, 2);
    cout << "t2 away!" << endl;

    t1.join(); //join
    cout << "thread 1 joined" << endl;
    t2.join();
    cout << "thread 2 joined" << endl;

    return 0;
}
4

1 回答 1

7

混合 OpenMP 和任何其他线程库(pthreads、Win32 线程等)可能不是一个好主意。编写 OpenMP 运行时可能会假定它完全控制线程并且可能不支持parallel同时运行的区域(例如,它可能使用信号量等全局变量来控制线程池)。

实现这一点的更好的纯 OpenMP 方法是:

#include <iostream>
#include <omp.h>

using namespace std;

void hello(int a) {

    #pragma omp parallel for
    for (int i=0;i<5;++i) {
        #pragma omp critical
        cout << "Hello from " << a << "! " << "OMP thread iter " << i << endl;
    }

    cout << "About to return from hello function" << endl;
}

int main (int argc, char ** argv) {

    omp_set_nested(1);

    #pragma omp parallel sections num_threads(2)
    {
       #pragma omp section
       {
           hello(1);
       }
       #pragma omp section
       {
           hello(2);
       }
    }

    return 0;
}

需要调用 toomp_set_nested()才能启用默认禁用的嵌套并行性。

于 2012-11-02T15:46:36.277 回答