omp_demo.cpp (493B)
1 #include <iostream> 2 #include <omp.h> 3 4 int main(void) 5 { 6 for (int i = 1; i <= 4; ++i) 7 { 8 std::cout << "Loop counter = " << i << ":\n"; 9 #pragma omp parallel num_threads(i) if (parallel: i > 2) 10 { 11 const int tid = omp_get_thread_num(); // get my ID 12 // the next line is a critical section! (std::cout is not 13 // thread-safe) 14 #pragma omp critical 15 std::cout << "\tHello from thread " << tid << '\n'; 16 } 17 } 18 return 0; 19 }