omp threadprivate

Specifies a list of globally-visible variables that will be allocated private to each thread.

Syntax

#pragma omp threadprivate ( list)

Arguments

list

A comma-separated list of variables you want to be private to a thread.

Description

A variable listed in this pragma is allocated private to each thread in each parallel team.

Example: Incrementing the counter whenever the thread executes an iteration of the parallel loop

int counter;
#pragma omp threadprivate(counter);
  int N, i;
  int *a, *b, *c;
  allocate_and_initialize(a, b, c, N);

#pragma omp parallel for { counter = 0; }

#pragma omp parallel for schedule(dynamic)
  for(i=0; i<N; i++) {
        a[i] = b[i] + c[i];
        counter++;
}
 deallocate(a, b, c, N);
For more complete information about compiler optimizations, see our Optimization Notice.