In my code, I want to avoid throwing exceptions from inside any openMP parallel region (because this results in an unhandled exception if not caught within the same region). To this end, I tried to use the openmp run-time library function

omp_in_parallel();

to decide whether to throw an exception or write out an error message and terminate. However, under gcc 4.7.0 this will not work if there is only one thread to a parallel region:

#include <iostream>
#include <omp.h>

void do_something()
{
  if(!omp_in_parallel())           // omp_in_parallel() returns false!
    throw 3;                       // so should be able to safely throw
}

int main()
{
  omp_set_num_threads(1);
  try {
#   pragma omp parallel
    do_something();
  } catch(int e) {
    std::cerr<<"error: '"<<e<<"'\n";  // never gets here
  }
}

does not result in error: '3' but in terminate called after throwing an instance of 'int' Abort.

Is this the correct behaviour (of omp_in_parallel())? (the openMP standard appears sufficiently vague) or a bug in gcc? How can I fix the above code for do_something() so that it throws only when not in a parallel region?

Edit
Report