0

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 :)

Mr. Moose
  • 101
  • 8
  • What do you really return from the thread? Please try to create a [mcve] to show us. – Some programmer dude May 11 '20 at 13:44
  • [Here is your answer](https://stackoverflow.com/questions/2251452/how-to-return-a-value-from-pthread-threads-in-c) also you can check [here](https://docs.oracle.com/cd/E19253-01/816-5137/tlib-24/index.html) – ajayg2808 May 11 '20 at 14:02
  • What is `splitArray`? What is the result you get? What is the result you expected? What is the input that gives that result? Please take some time to refresh [ask] as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude May 11 '20 at 14:07

1 Answers1

0

The thread function is supposed to be returning a void * pointer, and on a 64-bit machine void * is typically 64 bits wide, while int is typically only 32 bits wide.

When you return a 32-bit value from a function that is expected to return a 64-bit value, what will the extra 32 bits have for value? That's unknown and your code exhibits undefined behavior because of this.

To solve your problem (as I think it might be) you need to update the thread function to return the correct type (void *) and do some casting to make sure the value you return is of the correct type and size:

void *countPrime(int array[]) {
    ...
    return (void *) (intptr_t) numPrimes;
}

Then when you fetch the value, you need to use the correct types as well, and use the opposite casting:

void *result;
for (int i = 0; i < n; i++) {
    pthread_join(threads[i],&result);
    numPrimes = numPrimes +  (int) (intptr_t) result;
}

Note that returning a non-pointer value like this (as well as passing a non-pointer value as argument to the thread function) is almost the only case where most people would agree that such casting is okay. Otherwise, it generally never is.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621