Export (0) Print
Expand All

parallel 

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


#pragma omp parallel [clauses]
{
      code_block
}

where,

clause (optional)

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

The parallel directive supports the following OpenMP clauses:

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

For more information, see 2.3 parallel Construct.

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

Sample Output

Hello from thread 0
Hello from thread 1
Hello from thread 2
Hello from thread 3

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

Community Additions

ADD
Show:
© 2014 Microsoft