scenario_2.cpp (951B)
1 #include <iostream> 2 #include <omp.h> 3 4 // shared variables 5 int A = 0; 6 int B = 0; 7 8 int main(void) 9 { 10 #pragma omp parallel num_threads(2) // use 2 threads 11 { 12 const int tid = omp_get_thread_num(); // my ID 13 if (0 == tid) { 14 A = 1; 15 // the following pragma necessarily serializes the code -> 16 // sequential consistency (synchronization primitive) 17 #pragma omp barrier 18 const int x = B; 19 20 #pragma omp critical // avoid garbled output (only for cosmetics) 21 std::cout << "Thread 0: x = " << x << std::endl; 22 } else { 23 B = 1; 24 // the following pragma necessarily serializes the code -> 25 // sequential consistency (synchronization primitive) 26 #pragma omp barrier 27 const int y = A; 28 29 #pragma omp critical // avoid garbled output (only for cosmetics) 30 std::cout << "Thread 1: y = " << y << std::endl; 31 } 32 } 33 return 0; 34 }