Jim

threads prog2

No preview for this file type
#include <stdio.h>
#include <pthread.h>
#define STEP 4000000
void* fibonacci_work(void* sum_ptr){
long int sum;
int one, two, three;
one=1;
two=2;
three = 0;
sum = 0;
while (two <= STEP){
three = one + two;
// printf ("%i,", three);
if ( (two % 2 ) == 0)
sum += two;
// printf ("sum is now: %lu\n", sum);
one = two;
two = three;
}
pthread_exit((void*) sum);
}
int main () {
pthread_t thread1;
pthread_attr_t attr;
long sum;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
if (pthread_create (&thread1, &attr, fibonacci_work, NULL)){
fprintf (stderr, "Error creating thread\n");
return 1;
}
pthread_attr_destroy(&attr);
if (pthread_join (thread1,(void **)&sum)){
fprintf(stderr, "Error joining thread\n");
return 2;
}
printf("sum: %lu\n", sum);
return 0;
}
... ...