Hi, I am looking ofr pointers to correct the following errors in a C++ program to generate user specified number of values of the Fibonacci sequence and I have to use threads. I am new to threads. Any help is appreciated!
osproj2b.cpp: In function âint main()â:
osproj2b.cpp:23: error: invalid conversion from âvoid*â to âvoid* (*)(void*)â
osproj2b.cpp:23: error: initializing argument 3 of âint pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)â
osproj2b.cpp:26: error: invalid type argument of âunary *â
osproj2b.cpp:29: error: name lookup of âiâ changed for new ISO âforâ scoping
osproj2b.cpp:26: error: using obsolete binding at âiâ
osproj2b.cpp: In function âvoid* CalculateFibonacci(void*)â:
osproj2b.cpp:51: error: new declaration âvoid* CalculateFibonacci(void*)â
osproj2b.cpp:9: error: ambiguates old declaration âvoid CalculateFibonacci(void*)â
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <iostream>
#include <cstdlib>
void HowMany(int *numbers);
void CalculateFibonacci(void *numbers);
std::vector<int> fibValues;
int main() {
int numbers = 0;
pthread_t child;
pthread_attr_t attr;
pthread_attr_init(&attr);
HowMany(&numbers);
pthread_create(&child, &attr, CalculateFibonacci, (void*) &numbers);
pthread_join(child, NULL);
for(int i = 0; i < numbers - 1; i++ )
printf("%d, ", fibValues.at(i));
printf("%d\n", fibValues.at(i));
return 0;
}
void HowMany(int *numbers)
{
// use pointers as parameter, not references
std::cout << "How many Fibonacci numbers would you like to calculate? ";
std::cin >> *numbers;
if ((!std::cin.good()) || ( *numbers <= 1 || ( *numbers >= 48 )))
{
printf("Invalid number entered ! I calculate only more than 0 or less then 48 Fibonacci numbers !! \n");
exit(1);
}
}
void *CalculateFibonacci(void *param)
{
int i = 0;
int *pointer;
pointer = (int *) param;
int fibCount = *pointer;
int a = 1;
int b = 0;
int fibonacci_number = 0;
while( i < fibCount )
{
b = fibonacci_number;
fibonacci_number = a + b;
fibValues.push_back(fibonacci_number);
a = b;
i++;
}
pthread_exit(0);
}
|