mirror of
https://github.com/OpenMP/Examples.git
synced 2025-04-04 05:41:33 +01:00
107 lines
2.9 KiB
TeX
107 lines
2.9 KiB
TeX
\pagebreak
|
|
\chapter{The \code{threadprivate} Directive}
|
|
\label{chap:threadprivate}
|
|
|
|
The following examples demonstrate how to use the \code{threadprivate} directive
|
|
to give each thread a separate counter.
|
|
|
|
\cexample{threadprivate}{1c}
|
|
|
|
\fexample{threadprivate}{1f}
|
|
|
|
\ccppspecificstart
|
|
The following example uses \code{threadprivate} on a static variable:
|
|
|
|
\cnexample{threadprivate}{2c}
|
|
|
|
The following example demonstrates unspecified behavior for the initialization
|
|
of a \code{threadprivate} variable. A \code{threadprivate} variable is initialized
|
|
once at an unspecified point before its first reference. Because \code{a} is
|
|
constructed using the value of \code{x} (which is modified by the statement
|
|
\code{x++}), the value of \code{a.val} at the start of the \code{parallel}
|
|
region could be either 1 or 2. This problem is avoided for \code{b}, which uses
|
|
an auxiliary \code{const} variable and a copy-constructor.
|
|
|
|
\cnexample{threadprivate}{3c}
|
|
\ccppspecificend
|
|
|
|
The following examples show non-conforming uses and correct uses of the \code{threadprivate}
|
|
directive.
|
|
|
|
\fortranspecificstart
|
|
The following example is non-conforming because the common block is not declared
|
|
local to the subroutine that refers to it:
|
|
|
|
\fnexample{threadprivate}{2f}
|
|
|
|
The following example is also non-conforming because the common block is not declared
|
|
local to the subroutine that refers to it:
|
|
|
|
\fnexample{threadprivate}{3f}
|
|
|
|
The following example is a correct rewrite of the previous example:
|
|
% blue line floater at top of this page for "Fortran, cont."
|
|
\begin{figure}[t!]
|
|
\linewitharrows{-1}{dashed}{Fortran (cont.)}{8em}
|
|
\end{figure}
|
|
|
|
\fnexample{threadprivate}{4f}
|
|
|
|
The following is an example of the use of \code{threadprivate} for local variables:
|
|
|
|
\fnexample{threadprivate}{5f}
|
|
% blue line floater at top of this page for "Fortran, cont."
|
|
\begin{figure}[t!]
|
|
\linewitharrows{-1}{dashed}{Fortran (cont.)}{8em}
|
|
\end{figure}
|
|
|
|
The above program, if executed by two threads, will print one of the following
|
|
two sets of output:
|
|
|
|
\code{a = 11 12 13}
|
|
\\
|
|
\code{ptr = 4}
|
|
\\
|
|
\code{i = 15}
|
|
|
|
\code{A is not allocated}
|
|
\\
|
|
\code{ptr = 4}
|
|
\\
|
|
\code{i = 5}
|
|
|
|
or
|
|
|
|
\code{A is not allocated}
|
|
\\
|
|
\code{ptr = 4}
|
|
\\
|
|
\code{i = 15}
|
|
|
|
\code{a = 1 2 3}
|
|
\\
|
|
\code{ptr = 4}
|
|
\\
|
|
\code{i = 5}
|
|
|
|
The following is an example of the use of \code{threadprivate} for module variables:
|
|
|
|
\fnexample{threadprivate}{6f}
|
|
\fortranspecificend
|
|
|
|
\ccppspecificstart
|
|
The following example illustrates initialization of \code{threadprivate} variables
|
|
for class-type \code{T}. \code{t1} is default constructed, \code{t2} is constructed
|
|
taking a constructor accepting one argument of integer type, \code{t3} is copy
|
|
constructed with argument \code{f()}:
|
|
|
|
\cnexample{threadprivate}{4c}
|
|
|
|
The following example illustrates the use of \code{threadprivate} for static
|
|
class members. The \code{threadprivate} directive for a static class member must
|
|
be placed inside the class definition.
|
|
|
|
\cnexample{threadprivate}{5c}
|
|
\ccppspecificend
|
|
|