I have a problem with my C-program and I think I need some help. My program is doing some calculations using multiple threads. Every thread runs a method with only one parameter and in the end returns an integer.
Now, to complete my calculation, it is necessary to take the sum of all sub-calculations, that means the sum of all integers returned by the threads.
But somehow, that result is not correct. I think I made a mistake in getting all returned integers from the threads. Here is my code:
//creating the threads (with splitArray[] as an array of pointers to other arrays)
pthread_t threads[n];
for (int i = 0; i < n; i++) {
pthread_create(&threads[i], NULL, (void * )(countPrime), (void * )splitArray[i]);
}
//getting the results of the threads
int numPrimes = 0;
int save;
for (int i = 0; i < n; i++) {
pthread_join(threads[i],(void **) &save);
numPrimes = numPrimes + save;
}
This is the method every given to every thread:
int countPrime(int array[]) {
int numPrimes = 0;
for (int i = 0; i < size; i++) {
//checking if array[i] is a prime number
if (isPrime(array[i])) {
numPrimes++;
}
}
return numPrimes;
}
Have I made a mistake? I am new to C and so I am not really confident about working with pointers, which seems to be necessary in this case.
Thanks a lot :)