Visual C ++只有一个线程在工作(OpenMP)

Mar*_*rto 0 c++ multithreading openmp visual-c++

我有一个用于多线程应用程序的简单代码,但是无论我传递它的线程数是多少,它都只能使用一个线程执行。最初,我认为这是计算机的问题,但是我尝试了另一台计算机,但是它也无法正常工作。我有什么事吗 如果有帮助,我正在使用Visual Studio 2015。

int th_id, nthreads;
#pragma omp parallel private(th_id) shared(nthreads) num_threads(3)
{
    th_id = omp_get_thread_num();
    #pragma omp critical
    {
        cout << "Hello World from thread " << th_id << '\n';
    }
    #pragma omp barrier

    #pragma omp master
    {
        nthreads = omp_get_num_threads();
        cout << "There are " << nthreads << " threads" << '\n';
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

您是否在项目属性对话框中激活了OpenMP支持?您可以在“配置属性-> C / C ++->语言”下找到它。