// use threads to find the cumulative sums 
// in the array x; for simplicity, assume 
// nthreads evenly divides n 

#include <stdio.h>
#include <pthread.h>  

#define MAX_N 100000000
#define MAX_THREADS 25

// shared variables
int nthreads,  // number of threads (not main())
    n,  
    x[MAX_N+1];
pthread_t id[MAX_THREADS];
pthread_barrier_t barr;

main(int argc, char **argv)
{  int i;
   n = atoi(argv[1]);
   nthreads = atoi(argv[2]);
   pthread_barrier_init(&barr,NULL,nthreads); 
   for (i = 0; i < n; i++)  
      x[i] = i;  // just a test
   for (i = 0; i < nthreads; i++)  
      // i is the thread number (0,1,...)
      pthread_create(&id[i],NULL,worker,i);
   for (i = 0; i < nthreads; i++)
      pthread_join(id[i],NULL);
   printf("cumulative sums:\n");
   for (i = 0; i < n; i++) printf("%d\n",x[i]);
}
