UNB/ CS/ David Bremner/ teaching/ cs3383/ lectures/ 41.1-demos/ spawn-norace.c
/* compile with -fopenmp */
#include <stdio.h>

long sum(long i, long j) {
  if (i>j) return 0;
  if (i==j) {
    return i;
  } else {
    long left,right,m=(i+j)/2;
#pragma omp task shared(left)
    left = sum(i,m);
    right = sum(m+1,j);
#pragma omp taskwait
    return left+right;
  }
}

int main(int argc, char** argv){

  long x;

#pragma omp parallel
  {  
    x = sum(1,10000);
  }

  printf("x = %ld\n", x);
  }