C | Fortran-90



The barrier construct, which is a stand-alone directive, specifies an explicit synchronisation barrier at the point at which the construct appears. The barrier applies to the innermost enclosing parallel region, forcing every thread that belong to the team of that parallel region to complete any pending explicit task. Only once all threads of that team satisfy this criterion will they be allowed to continue their execution beyond the barrier.



#pragma omp barrier <new-line>




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

 * @brief Illustrates the OpenMP barrier synchronisation.
 * @details This application is made of a parallel region, in which two distinct
 * parts are to be executed, separated with a barrier. In each part, threads
 * have to print a message. They will print their second message only when all
 * threads will have printed the first one.
int main(int argc, char* argv[])
    // Use 4 threads when we create a parallel region

    // Create the parallel region
    #pragma omp parallel
        // Threads print their first message
        printf("[Thread %d] I print my first message.\n", omp_get_thread_num());

        // Make sure all threads have printed their first message before moving on.
        #pragma omp barrier

        // One thread indicates that the barrier is complete.
        #pragma omp single
            printf("The barrier is complete, which means all threads have printed their first message.\n");

        // Threads print their second message
        printf("[Thread %d] I print my second message.\n", omp_get_thread_num());

    return EXIT_SUCCESS;