# THREADPRIVATE

OpenMP* Fortran Compiler Directive: Specifies named common blocks and certain variables to be private (local) to each thread; they are global within the thread.

!$OMP THREADPRIVATE (list)

list | Is a comma-separated list of named common blocks, module variables, or variables that have the SAVE attribute. These objects are made private to a thread. Note that common blocks must appear between slashes (/). A blank common block cannot appear in a THREADPRIVATE directive. A variable that appears in a THREADPRIVATE directive must be declared in the scope of a module or have the SAVE attribute, either explicitly or implicitly.

Each thread gets its own copy of the common block or variable, so data written to this object by one thread is not directly visible to other threads.

During serial portions and MASKED sections of the program, accesses are to the primary thread copy of the common block or variable. On entry to the first parallel region, data in the THREADPRIVATE common blocks or variables should be assumed to be undefined unless a COPYIN clause is specified in the PARALLEL directive.

The THREADPRIVATE directive must appear in the section of a scoping unit in which the common block or variable is declared. Although variables in common blocks can be accessed by use association or host association, common block names cannot. This means that a common block name specified in a THREADPRIVATE directive must be declared to be a common block in the same scoping unit in which the THREADPRIVATE directive appears.

When a common block (which is initialized using DATA statements) appears in a THREADPRIVATE directive, each thread copy is initialized once prior to its first use. For subsequent parallel regions, data in THREADPRIVATE common blocks are guaranteed to persist only if the dynamic threads mechanism has been disabled and if the number of threads are the same for all the parallel regions.

A THREADPRIVATE common block or its constituent variables can appear only in a COPYIN clause. They are not permitted in a PRIVATE, FIRSTPRIVATE, LASTPRIVATE, SHARED, or REDUCTION clause. They are not affected by the DEFAULT clause.

If a THREADPRIVATE directive specifying a common block name appears in one program unit, then the directive must also appear in every other program unit that contains a COMMON statement specifying the same common block name. It must appear after the last COMMON statement in the program unit.

If a THREADPRIVATE variable or a THREADPRIVATE common block is declared with the BIND attribute, the corresponding C entities must also be specified in a threadprivate directive in the C program.

The following are other restrictions for a THREADPRIVATE variable:

- The variable must not appear in any clause except the COPYIN, COPYPRIVATE, SCHEDULE, NUM_THREADS, THREAD_LIMIT, and IF clauses.

- It is affected by a COPYIN clause if the variable appears in the COPYIN clause or it is in a common block that appears in the COPYIN clause.

- It must not be an element of a common block or appear in an EQUIVALENCE statement.

- If it is part of another variable (as an array or structure element), it cannot appear in a THREADPRIVATE directive clause.

### Note

On Windows* systems, if you specify option /Qopenmp-threadprivate:compat, the compiler does not generate threadsafe code for common blocks in an !$OMP THREADPRIVATE directive unless at least one element in the common block is explicitly initialized.

## Example

In the following example, the common blocks BLK1 and FIELDS are specified as thread private:

            COMMON /BLK/ SCRATCH
            COMMON /FIELDS/ XFIELD, YFIELD, ZFIELD
      !$OMP THREADPRIVATE(/BLK/,/FIELDS/)
      !$OMP PARALLEL DEFAULT(PRIVATE) COPYIN(/BLK1/,ZFIELD)

Parent topic: T to Z

## See Also

OpenMP Fortran Compiler Directives
Syntax Rules for Compiler Directives