A.26 Using the threadprivate Directive

The following examples demonstrate how to use the threadprivate directive (Section 2.7.1 on page 23) to give each thread a separate counter.

Example 1:

int counter = 0;
#pragma omp threadprivate(counter)

int sub()
{
    counter++;
    return(counter);
}

Example 2:

int sub()
{
    static int counter = 0;
    #pragma omp threadprivate(counter)
    counter++;
    return(counter);
}