|
#include <omp.h> int main(int argc, char *argv[]) { int N; // Variable defined OUTSIDE parallel region.... // It is therefore SHARED N = 0; #pragma omp parallel { int i; for ( i = 0; i < 10000; i++ ) N = N + 1; cout << "N = " << N << endl; } } |
You should see the value for N at the end is not always 1009, it could be less. You have seen this phenomenon before in threaded programs when multiple-threads update a shared variable concurrently... The same things is happening here.
#include <omp.h> int main(int argc, char *argv[]) { #pragma omp parallel { int N = 0; // Variable defined INSIDE parallel region.... // It is therefore NON-SHARED int i; for ( i = 0; i < 10000; i++ ) N = N + 1; cout << "N = " << N << endl; } // ERROR if you try to do this: // cout << "N = " << N << endl; // because N is not defined in the outer scope !!! } |