Worksharing Constructs Inside a critical Construct
9.2. Worksharing Constructs Inside a critical Construct#
The following example demonstrates using a worksharing construct inside a critical construct. This example is conforming because the worksharing single region is not closely nested inside the critical region. A single thread executes the one and only section in the sections region, and executes the critical region. The same thread encounters the nested parallel region, creates a new team of threads, and becomes the primary thread of the new team. One of the threads in the new team enters the single region and increments i by 1. At the end of this example i is equal to 2.
//%compiler: clang
//%cflags: -fopenmp
/*
* name: worksharing_critical.1
* type: C
*/
void critical_work()
{
int i = 1;
#pragma omp parallel sections
{
#pragma omp section
{
#pragma omp critical (name)
{
#pragma omp parallel
{
#pragma omp single
{
i++;
}
}
}
}
}
}
!!%compiler: gfortran
!!%cflags: -fopenmp
! name: worksharing_critical.1
! type: F-fixed
SUBROUTINE CRITICAL_WORK()
INTEGER I
I = 1
!$OMP PARALLEL SECTIONS
!$OMP SECTION
!$OMP CRITICAL (NAME)
!$OMP PARALLEL
!$OMP SINGLE
I = I + 1
!$OMP END SINGLE
!$OMP END PARALLEL
!$OMP END CRITICAL (NAME)
!$OMP END PARALLEL SECTIONS
END SUBROUTINE CRITICAL_WORK