I am just starting to learn about threads, and I am trying to write a thread to compute fibonacci and return an indicated fibonacci number. I have to use pthread_join to wait and collect the returned value, and then print it.
Hereis what I have so far, but I am stuck, it keeps giving me the error
" fibon.c: (.text+0x65): undefined reference to `fibonacci'
collect2: ld returned 1 exit status"
Please help me!
Code:
#include <pthread.h>
#include <stdio.h>
/* compute sucessive fibonnaci numbers.*/
void* fibonacci(void* arg);
main()
{
pthread_t thread;
int num1 = 0;
int num2 = 1;;
int ans;
int which_fibo;
int counter, numPrint;
numPrint = 10;
for(counter= 0; counter < numPrint; ++counter){
ans = (num1 + num2);
num1 = num2;
num2 = ans;
}
/* start the computing thread up to the number you want to return*/
pthread_create(&thread, NULL, &fibonacci, (void*)&which_fibo);
/* wait for the fibonacci thread to complete */
pthread_join(thread, (void*)&ans);
/* print the number in the position requested */
printf("the number is %d.\n",which_fibo, ans);
}
