爲什麼我沒有得到不同的線程ID,當我使用「的#pragma OMP並行NUM_THREADS(4)」。在這種情況下,所有的線程ID都是0。 但是,當我評論該行並使用默認線程數時,我得到了不同的線程ID。 注意: - 變量我使用變量tid來獲取線程ID。的OpenMP:爲什麼我沒有得到不同的線程ID,當我使用「的#pragma OMP並行NUM_THREADS(4)」
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
int nthreads, tid;
int x = 0;
#pragma omp parallel num_threads(4)
#pragma omp parallel private(nthreads, tid)
{
/* Obtain thread number */
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
// /* Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
}
}
輸出的上面的代碼: -
Hello World from thread = 0
Hello World from thread = 0
Number of threads = 1
Hello World from thread = 0
Number of threads = 1
Hello World from thread = 0
Number of threads = 1
Number of threads = 1
輸出時我評論上述行: -
Hello World from thread = 3
Hello World from thread = 0
Number of threads = 4
Hello World from thread = 1
Hello World from thread = 2
注: - 編制我做 gcc -fopenmp open.c -o你好 –