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_static.cpp (509B)


      1 #include <iostream>
      2 #include <omp.h>
      3 
      4 int main(void)
      5 {
      6 #pragma omp parallel
      7     {
      8         static int static_tid; // static storage duration -> shared
      9 
     10         const int tid = omp_get_thread_num();
     11 #pragma omp single
     12         {
     13             static_tid = tid;
     14         } // implicit barrier required here -> no race condition below!
     15 
     16 #pragma omp critical
     17         {
     18             std::cout << "Thread " << tid
     19                       << ":\treading static_tid = " << static_tid << '\n';
     20         }
     21     }
     22     return 0;
     23 }