1

I'm not familiar with returning things from pthread start routines, so I've come to SO for some assistance.

The start routine will calculate some prime numbers of a given range, store them in an array of ints and then return that array to the main where it will be printed.

If there is a different way to go about accomplishing this, I'd love to hear it!

Here's what I've got:

//start routine
void *threadCreate(void* arg){
    int threadNumber = threadCount;
    threadCount++;
    Data *data;
    data = (struct Data*)arg;
    int primes[data->end]; //I don't know how many primes we will have, but I think if I use the end range value as a size it will be more than enough.
    int k = 0; //Index of the int array
    printf("Thread #%d results: ", threadNumber);
    for (int i = data->start; i <= data->end; i++){
        if (isPrime(i)){
            printf("%d ", i);
            primes[k] = i;
            k++;
        }
    }
    printf("\n");
    pthread_exit((void*)primes);
}

//in main, this is where we print our array
//I don't know the correct way to get this array
void *retval;

pthread_join(tid, &retval);

//im aware the next part is more than likely incorrect, I am just trying to illustrate what I am trying to do

for (int i = 0; i < len((int [])retval); i++){
   printf("%d ", (int[])retval[i]);
}      
Talen Kylon
  • 1,908
  • 7
  • 32
  • 60
  • 1
    Allocate the array of primes in the thread and add it as a member in your `Data` you will likely want an count/size or somesuch too. Also, if you are using multiple threads then be careful with whose data is whose. Also, remember to free the data in the parent thread. – SilverCode Nov 28 '13 at 23:50
  • 1
    That code will return the address of an array sitting in the stack. The array won't be there once it gets back to the calling code. – John3136 Nov 28 '13 at 23:50
  • http://stackoverflow.com/questions/2251452/how-to-return-a-value-from-thread-in-c – Ciro Santilli OurBigBook.com Jun 14 '15 at 21:28

1 Answers1

3

The pointer you return cannot be to an array with automatic storage duration in the thread function, because it will be destroyed once the thread function returns. You can use dynamic allocation, though. The main function will also need to know how many numbers are in the returned array - the easiest way to do this is to use zero as a sentinel, since the primality of zero is undefined.

int *primes = malloc((data->end + 1) * sizeof primes[0]);

if (primes)
{
    int k = 0; //Index of the int array

    for (int i = data->start; i <= data->end; i++)
    {
        if (isPrime(i))
        {
            printf("%d ", i);
            primes[k] = i;
            k++;
        }
    }

    primes[k] = 0; /* Add sentinel to mark the end */
}

pthread_exit(primes);

Then in the main function:

void *retval;
int *primes;

pthread_join(tid, &retval);
primes = retval;

if (primes != NULL)
{
    for (int i = 0; primes[i] != 0; i++)
    {
       printf("%d ", primes[i]);
    }
}
else
{
    /* Thread failed to allocate memory for the result */
}

free(primes);

You could also just allocate an array for the results within the Data struct that you pass to the thread function, and have it fill it in there.

caf
  • 233,326
  • 40
  • 323
  • 462