# BARRIER

OpenMP* Fortran Compiler Directive: Synchronizes all the threads in a team. It causes each thread to wait until all of the other threads in the team have reached the barrier.

!$OMP BARRIER

The binding thread set for a BARRIER construct is the current team. A barrier region binds to the innermost enclosing parallel region.

Each barrier region must be encountered by all threads in a team or by none at all, unless cancellation has been requested for the innermost enclosing parallel region.

The barrier region must also be encountered in the same order by all threads in a team.

## Example

    INTEGER K
    K = 17

    !$OMP PARALLEL SHARED (K) NUM_THREADS (2)
    IF (OMP_GET_THREAD_NUM() == 0) THEN
        X = 5
    ELSE
        ! The following read access of K creates a race condition
        PRINT *,"1: THREAD# ", OMP_GET_THREAD_NUM (), "K = ", K
    ENDIF

    ! This barrier contains implicit flushes on all threads, as well as a thread
    ! synchronization: this guarantees that the value 5 will be printed by
    ! both PRINT 2 and PRINT 3 below.

    !$OMP BARRIER

    IF (OMP_GET_THREAD_NUM() == 0) THEN
        PRINT *,"2: THREAD# ", OMP_GET_THREAD_NUM (), "K = ", K
    ELSE
        PRINT *,"3: THREAD# ", OMP_GET_THREAD_NUM (), "K = ", K
    ENDIF
    !$OMP END PARALLEL

Parent topic: A to B

## See Also

OpenMP Fortran Compiler Directives
Syntax Rules for Compiler Directives
Nesting and Binding Rules
Parallel Processing Model for information about Binding Sets