共用方式為


parallel

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

Defines a parallel region, which is code that will be executed by multiple threads in parallel.

Syntax

#pragma omp parallel [clauses]  
{  
   code_block  
}  

Remarks

where,

clause (optional)
Zero or more clauses. See the Remarks section for a list of the clauses supported by parallel.

Remarks

The parallel directive supports the following OpenMP clauses:

parallel can also be used with the sections and for directives.

For more information, see 2.3 parallel Construct.

Example

The following sample shows how to set the number of threads and define a parallel region. By default, the number of threads is equal to the number of logical processors on the machine. For example, if you have a machine with one physical processor that has hyperthreading enabled, it will have two logical processors and, therefore, two threads.

// omp_parallel.cpp  
// compile with: /openmp   
#include <stdio.h>  
#include <omp.h>  
  
int main() {  
   #pragma omp parallel num_threads(4)  
   {  
      int i = omp_get_thread_num();  
      printf_s("Hello from thread %d\n", i);  
   }  
}  
Hello from thread 0  
Hello from thread 1  
Hello from thread 2  
Hello from thread 3  

Comment

Note that the order of output can vary on different machines.

See Also

Directives