Openmp sections

Christoph Bartoschek bartoschek@gmx.de
Sat Mar 29 17:27:00 GMT 2008


Hi,

why is the following code not executed in parallel:

#include <iostream>
#include <omp.h>
 
int main()
{
 
#pragma omp sections nowait
{
  #pragma omp section
  {
    int i = 0;
    while (true) {
      ++i;
      if (i % 1000000 == 0) {
         std::cerr << "1. Thread: " << omp_get_thread_num() 
                   << "  i = " << i << std::endl;
      } 
    }
  }

  #pragma omp section
  {
    int i;
    while (true) {
      ++i;
      if (i % 1000000 == 0) {
        std::cerr << "2. Thread: " << omp_get_thread_num()
                  << "  i = " << i << std::endl;
      }
    }
  }
}

}

I know that the usage of std::cerr is not threadsafe. But this is only to see 
that only one thread is running. If I remove it, there still only one thread 
running.

I use g++ 4.3.0 and compile with:

g++ -fopenmp test.C -lgomp

./a.out prints only messages from 1. Thread.

Greetings
Christoph



More information about the Gcc-help mailing list