Visual C++ 只有一个线程在工作(OpenMP) [英] Visual C++ has only one thread working (OpenMP)

查看:103
本文介绍了Visual C++ 只有一个线程在工作(OpenMP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于多线程应用程序的简单代码,但无论我传递了多少线程,它都只执行一个线程.起初,我认为这是我的电脑的问题,但我尝试了另一台,它也不起作用.我有什么问题吗?如果有帮助,我正在使用 Visual Studio 2015.

I have this simple code for a multithreading application, but no matter the number of threads I pass it, it only executes with one thread. Initially, I thought this was a problem with my computer, but I tried on another and it also didn't work. Am I going anything wrong? I am using Visual Studio 2015 if that helps.

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';
    }
}

推荐答案

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

Did you activate OpenMP support in the project properties dialog? You can find it under "Configuration Properties -> C/C++ -> Language".

这篇关于Visual C++ 只有一个线程在工作(OpenMP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆