trying to calculate fibonacci's number with threads. It always return 1. Any suggestions?
my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
/* compile with -lpthread */
void* fibo (void* param) {
int a,b,n;
pthread_t thread_a, thread_b;
int ret;
n = (int) param;
if (n>1)
{ pthread_create(&thread_a,NULL,fibo,(void*)(n-1));
pthread_create(&thread_b,NULL,fibo,(void*)(n-2));
pthread_join(thread_a,(void**)&a);
pthread_join(thread_b,(void**)&b);
ret=a+b;
}
else ret=1;
return (ret);
/*pthread_exit((void**)ret);*/
}
int main(int argc,char* argv[]) {
pthread_t thread_id;
int n,ret;
n=atoi(argv[1]);
pthread_create(&thread_id,NULL,fibo,(void*)n);
pthread_join(thread_id,(void**)&ret);
printf("s(%d)=%d\n",n,ret);
return 0;
}