\pagebreak
\section{\code{threadprivate} Directive}
\label{sec:threadprivate}

The following examples demonstrate how to use the \code{threadprivate} directive 
 to give each thread a separate counter.

\cexample{threadprivate}{1}

\fexample{threadprivate}{1}

\ccppspecificstart
The following example uses \code{threadprivate} on a static variable:

\cnexample{threadprivate}{2}

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.

\cppnexample{threadprivate}{3}
\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}{2}

The following example is also non-conforming because the common block is not declared 
local to the subroutine that refers to it:

\fnexample{threadprivate}{3}

The following example is a correct rewrite of the previous example:

\fnexample{threadprivate}{4}

The following is an example of the use of \code{threadprivate} for local variables:
% blue line floater at top of this page for "Fortran, cont."
\begin{figure}[t!]
\linewitharrows{-1}{dashed}{Fortran (cont.)}{8em}
\end{figure}

\fnexample{threadprivate}{5}

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:
% blue line floater at top of this page for "Fortran, cont."
\begin{figure}[t!]
\linewitharrows{-1}{dashed}{Fortran (cont.)}{8em}
\end{figure}

\fnexample{threadprivate}{6}
\fortranspecificend

\cppspecificstart
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()}:

\cppnexample{threadprivate}{4}

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.

\cppnexample{threadprivate}{5}
\cppspecificend

