Intel® C++ Compiler 16.0 User and Reference Guide

omp barrier

Specifies a point in the code where each thread must wait until all threads in the team arrive.

Syntax

#pragma omp barrier

Arguments

None

Description

Each thread that encounters this pragma must wait until all threads in the team have arrived. After the last thread of the team arrives, all threads are released and may continue execution of the enclosing parallel region.

The code for the enclosing parallel region must be arranged so that either all or none of the threads encounter the pragma.

Example

This following example demonstrates how to use this pragma to ensure that all threads have executed the first loop before executing the second loop:

#include <omp.h>

void work1(int k) {
  // large amount of work
}
void work2(int k) {
  // large amount of work that must all happen after work1 is finished
}

int main() {
  int n=1000000;
  #pragma omp parallel private(i) shared(n) {
    #pragma omp for
    for (i=0; i<n; i++)
    work1(i);

    #pragma omp barrier
    #pragma omp for
    for (i=0; i<n; i++)
    work2(i);
  }
 return 0;
}