cs205-lecture-examples

Example codes used during Harvard CS205 lectures
git clone https://git.0xfab.ch/cs205-lecture-examples.git
Log | Files | Refs | README | LICENSE

omp_firstprivate.cpp (574B)


      1 #include <iostream>
      2 #include <omp.h>
      3 
      4 int main(void)
      5 {
      6     int shared = -1; // shared variable
      7 
      8 #pragma omp parallel firstprivate(shared) // create private copy (initialized!)
      9     {
     10         const int tid = omp_get_thread_num();
     11 #pragma omp single
     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 }