Communicating between Threads and Sharing Data
OpenMP applications communicate between threads using variables. The OpenMP application begins with a master thread that has its own data environment. When the master thread encounters a parallel region, LabWindows/CVI creates new execution threads. Each new thread has its own data environment. Because all threads in a parallel region have their own data environment, you can let all the threads in a team share a copy of a variable or you can designate each thread a private copy of the variable. The variable designations are specific to each variable and to each parallel region, therefore a thread can have shared and private variables and you can change the designation of private or shared at each parallel region. The following clauses are the three primary OpenMP clauses you can use to scope variables in a parallel region:
- shared—Shared variables within a parallel region share a single storage location.
- private—Private variables within a parallel region have multiple storage locations, one location for each thread in the team.
- reduction—Variables you mark for a reduction have both private and shared variable behaviors. LabWindows/CVI creates a private variable for each thread and, at the end of the reduction of the private variables, copies the result to the original variable.
![]() |
Note LabWindows/CVI does not support variable-length arrays as data sharing variables on OpenMP clauses. |
The following code demonstrates the use of the private clause.
#ifdef _OPENMP
#include <omp.h>
#endif
#include <ansi_c.h>
#include <stdio.h>
int main (int argc, char *argv[])
{
int a;
void goo(int k) {
a = k; /* The global "a", not the private "a" in foo */
}
void foo(int n) {
int a = 0;
#pragma omp parallel for private(a)
for (int i=1; i<n; i++) {
a = i;
goo(a*2); /* Private copy of "a" */
}
Additionally, you can use the following data scoping attributes for more control of variables in parallel regions.
To designate private variables for threads at a global file scope, use the threadprivate directive.