#pragma omp 并行 num_threads 不起作用

use*_*234 7 c c++ parallel-processing openmp

    #include<omp.h>
    #include<stdio.h>
    #include<stdlib.h>

    void main(int argc, int *argv[]){


   #pragma omp parallel num_threads(3)
   {

    int tid = omp_get_thread_num();
    printf("Hello world from thread = %d \n",tid);
    if(tid == 0){
        int nthreads = omp_get_num_threads();
        printf("Number of threads = %d\n",nthreads);
    }
   }

  }
Run Code Online (Sandbox Code Playgroud)

我正在学习 OpenMP,但我不明白为什么当我指定了线程数 3 时它只执行一个线程?程序输出:

   Hello world from thread = 0
   Number of threads = 1
Run Code Online (Sandbox Code Playgroud)

che*_*rss 4

您需要使用 来编译您的程序-fopenmp

g++ a.cc -fopenmp
Run Code Online (Sandbox Code Playgroud)