I'm having trouble in the following exercise
Write a program that, given a vector with the first 5 numbers cousins, throw 5 threads. Each of them will calculate the value of the factorial of one of the positions of the vector and calculated value. The main thread expects all threads finishes, prints the vector and ends as well.
I have the following resolution
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#define NTHREADS 5
void *Fatorial(int *array)
{
printf("***Entrei na função fatorial***\n");
int fat = 0, n = 0;
for(fat = 1; n > 1; n = n - 1)
{
array[n] = array[n] * n;
}
pthread_exit(NULL);
}
int main()
{
printf("Início da função\n");
int *array[5] = {2,3,5,7,11};
int i = 0,rc = 0;
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
printf("Antes do FOR\n");
for(i = 0; i < NTHREADS; i++)
{
rc = array[i];
rc = pthread_create(&tid,&attr,Fatorial,NULL);
if (rc)
{
printf("ERROR - return code from pthread_create() is %d\n", rc);
exit(-1);
}
rc = pthread_join(tid, NULL);
if (rc)
{
printf("ERROR; return code from pthread_join() is %d\n", rc);
exit(-1);
}
}
printf("Array[] = ");
for(i = 0; i < NTHREADS; i++)
{
printf("%d ",array[i]);
}
pthread_attr_destroy(&attr);
pthread_exit(NULL);
return 0;
}
One of my problems is how to get the array to be calculated on the auxiliary thread (which is to calculate the factorial of an array position). Testing with the print's I can see that the program enters 5 times in the auxiliary thread so I think I'm on a correct path. I need to understand how to move the array itself.