sfa
sfa

Reputation: 111

OpenMP strange behaviour

Hello I have the following code, which I compile with gcc (>4.2) with -fopenmp flag:

int main(void)
{
#pragma omp parallel for
    int i; 
    for(i=0;i<4;i++) while(1);

    return 0;
}

I get a SIGSEGV on OSX Lion (ver 1.7.3, llvm-gcc 4.2.1) and CentOS 6.2 . What am I doing wrong here? Thanks

Upvotes: 7

Views: 499

Answers (3)

sfa
sfa

Reputation: 111

There was a bug in the gcc regarding this issue, I reported it and they will provide a fix. Here is the link: GCC bug

Upvotes: 1

spraff
spraff

Reputation: 33385

Not sure if this is relevant to the compiler version and configuration but while(true){} terminates

More precisely, if you write a loop which

  • makes no calls to library I/O functions, and
  • does not access or modify volatile objects, and
  • performs no synchronization operations (1.10) or atomic operations (Clause 29)

and does not terminate, you have undefined behaviour.

This may end up not applying to your situation, but as C++11 becomes more established, watch out.

Upvotes: 2

mikithskegg
mikithskegg

Reputation: 816

Very interesting.

I changed the code a little so

int main(void)
{
int i;
#pragma omp parallel 
  {
        while(1);
    }
    return 0;
} 

and so

inline void func() {
    while (1) ;
}

int main(void)
{
int i;
#pragma omp parallel for 
    for(i=0;i<8;i++) {
        func();
    }
    return 0;
}

And they both worked OK.

Upvotes: 1

Related Questions