STATIC (IBM extension)
Purpose
The STATIC attribute specifies that a variable has a storage class of static; that is, the variable remains in memory for the duration of the program and its value is retained between calls to the procedure.
Syntax
- stat_variable
- is a variable name or an array declarator that can specify an explicit_shape_spec_list or a deferred_shape_spec_list.
- initial_value
- provides an initial value for the variable specified by the immediately preceding name. Initialization occurs as described in DATA.
Rules
If stat_variable is a result variable, it must not be of type character or of derived type. Dummy arguments, automatic objects and pointees must not have the STATIC attribute. A variable that is explicitly declared with the STATIC attribute cannot be a common block item.
A variable must not have the STATIC attribute specified more than once in the same scoping unit.
Local variables have a default storage class of automatic. See the -qsave option in the XL Fortran Compiler Reference for details on the default settings with regard to the invocation commands.
Variables declared as STATIC are shared amongst threads. To thread-safe an application that contains shared variables, you must either serialize access to the static data using locks, or make the data thread-specific. One method of making the data thread-specific is to move the static data into a COMMON block that has been declared THREADLOCAL. The Pthreads library module provides mutexes to allow you to serialize access to the data using locks. See Pthreads library module in the XL Fortran Optimization and Programming Guide for more information. The lock_name attribute on the CRITICAL directive also provides the ability to serialize access to data. See CRITICAL /END CRITICAL for more information. The THREADLOCAL directive ensures that common blocks are local to each thread. See THREADLOCAL for more information.
| ALLOCATABLE 1 | POINTER | SAVE |
| ASYNCHRONOUS | PRIVATE | TARGET |
| CONTIGUOUS 2 | PROTECTED 1 | VOLATILE |
| DIMENSION | ||
Note:
|
||
Examples
LOGICAL :: CALLED=.FALSE.
CALL SUB(CALLED)
CALLED=.TRUE.
CALL SUB(CALLED)
CONTAINS
SUBROUTINE SUB(CALLED)
INTEGER, STATIC :: J
LOGICAL :: CALLED
IF (CALLED.EQV..FALSE.) THEN
J=2
ELSE
J=J+1
ENDIF
PRINT *, J ! Output on first call is 2
! Output on second call is 3
END SUBROUTINE
END



