Intel® Fortran Compiler 17.0 Developer Guide and Reference
OpenMP* Fortran Compiler Directive: Restricts access to a block of code to only one thread at a time.
!$OMP CRITICAL [(name)]
block
!$OMP END CRITICAL [(name)]
name |
Is the name of the critical section. |
block |
Is a structured block (section) of statements or constructs. You cannot branch into or out of the block. |
The binding thread set for a CRITICAL construct is all threads in the contention group. Region execution is restricted to a single thread at a time among all threads in the contention group, without regard to the teams to which the threads belong.
A thread waits at the beginning of a critical section until no other thread in the team is executing a critical section having the same name. All unnamed CRITICAL directives map to the same name.
If a name is specified in the CRITICAL directive, the same name must appear in the corresponding END CRITICAL directive. If no name appears in the CRITICAL directive, no name can appear in the corresponding END CRITICAL directive.
Critical section names are global entities of the program. If the name specified conflicts with any other entity, the behavior of the program is undefined.
The following example shows a queuing model in which a task is dequeued and worked on. To guard against multiple threads dequeuing the same task, the dequeuing operation is placed in a critical section.
Because there are two independent queues in this example, each queue is protected by CRITICAL directives having different names, XAXIS and YAXIS, respectively:
!$OMP PARALLEL DEFAULT(PRIVATE) SHARED(X,Y)
!$OMP CRITICAL(XAXIS)
CALL DEQUEUE(IX_NEXT, X)
!$OMP END CRITICAL(XAXIS)
CALL WORK(IX_NEXT, X)
!$OMP CRITICAL(YAXIS)
CALL DEQUEUE(IY_NEXT,Y)
!$OMP END CRITICAL(YAXIS)
CALL WORK(IY_NEXT, Y)
!$OMP END PARALLEL