UNB/ CS/ David Bremner/ teaching/ cs3383/ examples/ spawn-norace.cc
#include <iostream>
#include <cilk/cilk.h>
#include <cilk/cilk_api.h>

using namespace std;


long sum(long i, long j) {

  if (i>j) return 0;
  if (i==j) return 1;

  long m=(i+j)/2;

  long left = cilk_spawn sum(i,m);
  long right = sum(m+1,j);
  cilk_sync;
  return left + right;
}

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

  long x = 0;

  const char *worker_param = "4";

  if (argc > 1)
    worker_param = argv[1];
  
  if (0!= __cilkrts_set_param("nworkers",worker_param))  {
      printf("Failed to set worker count\n");
      return 1;
    }

  x = sum(1,100000000);

  cout << x << endl;
}