omp_private.cpp (578B)
1 #include <iostream> 2 #include <omp.h> 3 4 int main(void) 5 { 6 int shared = -1; // shared variable 7 8 #pragma omp parallel private(shared) // create private copy (uninitialized!) 9 { 10 const int tid = omp_get_thread_num(); 11 #pragma omp single nowait 12 { 13 shared = tid; 14 } // no need to wait -> `shared` is private -> no race condition below! 15 16 #pragma omp critical 17 { 18 std::cout << "Thread " << tid 19 << ":\tvalue of shared (private copy) = " << shared 20 << '\n'; 21 } 22 } 23 return 0; 24 }