Home | History | Annotate | Download | only in tests
      1 /* A simple example with 4 threads */
      2 
      3 #include <pthread.h>
      4 #include <unistd.h>
      5 
      6 double a[1000];
      7 
      8 static void init()
      9 {
     10 	int i;
     11 	for(i=0;i<1000;i++) a[i] = (double)i;
     12 }
     13 
     14 static void *th(void *v)
     15 {
     16 	double sum = 0.0;
     17 	int i,j;
     18 
     19 	for(j=0;j<1000;j++)
     20 		for(i=0;i<1000;i++)
     21 			sum += a[i];
     22 
     23 	*( (double*)v ) = sum;
     24 
     25 	/* make sure that no threads is so fast that it finishes
     26          * before last thread is created, thus reusing the TID */
     27 	sleep(1);
     28 
     29 	return 0;
     30 }
     31 
     32 int main()
     33 {
     34 	pthread_t t[4];
     35 	double sum[4];
     36 	int i;
     37 
     38 	init();
     39 
     40 	for(i=0;i<4;i++)
     41 		pthread_create(&t[i], NULL, th, &sum[i]);
     42 
     43 	for(i=0;i<4;i++)
     44 		pthread_join(t[i], NULL);
     45 
     46 	return 0;
     47 }
     48