XL Fortran for AIX 8.1

Language Reference

CRITICAL / END CRITICAL

Purpose

The CRITICAL construct allows you to define independent blocks of code that are to be run by at most one thread at a time. It includes a CRITICAL directive that is followed by a block of code and ends with an END CRITICAL directive.

The CRITICAL and END CRITICAL directives only take effect if you specify the -qsmp compiler option.

Format



>>-CRITICAL--+-----------------+-------------------------------><
             '-(--lock_name--)-'
 
 
>>-block-------------------------------------------------------><
 
 
>>-END CRITICAL--+-----------------+---------------------------><
                 '-(--lock_name--)-'
 
 

lock_name
provides a way of distinguishing different CRITICAL constructs of code.

block
represents the block of code to be executed by at most one thread at a time.

Rules

The optional lock_name is a name with global scope. You must not use the lock_name to identify any other global entity in the same executable program.

If you specify the lock_name on the CRITICAL directive, you must specify the same lock_name on the corresponding END CRITICAL directive.

If you specify the same lock_name for more than one CRITICAL construct, the compiler will allow only one thread to execute any one of these CRITICAL constructs at any one time. CRITICAL constructs that have different lock_names may be run in parallel.

The same lock protects all CRITICAL constructs that do not have an explicit lock_name. In other words, the compiler will assign the same lock_name, thereby ensuring that only one thread enters any unnamed CRITICAL construct at a time.

The lock_name must not share the same name as any local entity of Class 1. For the definition of a local entity of Class 1, see Local Entity.

It is illegal to branch into or out of a CRITICAL construct.

The CRITICAL construct may appear anywhere in a program.

Although it is possible to nest a CRITICAL construct within a CRITICAL construct, a deadlock situation may result. The -qsmp=rec_locks compiler option can be used to prevent deadlocks. See User's Guide for more information.

The CRITICAL and END CRITICAL directives imply the FLUSH directive.

Examples

Example 1: Note that in this example the CRITICAL construct appears within a DO loop that has been marked with the PARALLEL DO directive.

      EXPR=0
!SMP$ PARALLEL DO PRIVATE (I)
      DO I = 1, 100
!SMP$   CRITICAL
          EXPR = EXPR + A(I) * I
!SMP$   END CRITICAL
      END DO

Example 2: An example specifying a lock_name on the CRITICAL construct.

!SMP$ PARALLEL DO PRIVATE(T)
      DO I = 1, 100
        T = B(I) * B(I-1)
!SMP$   CRITICAL (LOCK)
          SUM = SUM + T
!SMP$   END CRITICAL (LOCK)
      END DO

Related Information


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]