tags:

views:

173

answers:

5

Hello,

I'am new to C and would like to play with threads a bit. I would like to return some value from a thread using pthread_exit()

My code is as follows:

#include <pthread.h>
#include <stdio.h>

void *myThread()
{
   int ret = 42;
   pthread_exit(&ret);
}

int main()
{
   pthread_t tid;
   void *status;

   pthread_create(&tid, NULL, myThread, NULL);
   pthread_join(tid, &status);

   printf("%d\n",*(int*)status);   

   return 0;
}

I would expect the program output "42\n" but it outputs a random number. How can I print the returned value?

EDIT: According to first answers the problem is that I am returning pointer to local variable. What is the best practice of returning/storing variables of multiple threads? A global hash table?

Thanks in advance

+2  A: 

I think you have to store the number on heap. The int ret variable was on stack and was destructed at the end of execution of function myThread.

void *myThread()
{
       int *ret = malloc(sizeof(int));
       *ret = 42;
       pthread_exit(ret);
}

Don't forget to free it when you don't need it :)

Another solution is to return the number as value of the pointer, like Neil Butterworth suggests.

Messa
It's stupid that pthread_exit accepts only pointer as parameter then :(
Petr Peller
It's not "stupid", it's a limitation of the C type system. In order to return a parameter of arbitrary type back though the pthreads system you either need a Python-style object system, where variables don't have types, only values do, or else you need a lot of C++ template technology. In exactly the same way, you can only return `void*` from the thread entry point, you can't return (e.g.) a double.
Steve Jessop
+2  A: 

You've returned a pointer to a local variable. That's bad even if threads aren't involved.

The usual way to do this, when the thread that starts is the same thread that joins, would be to pass a pointer to an int, in a location managed by the caller, as the 4th parameter of pthread_create. This then becomes the (only) parameter to the thread's entry-point. You can (if you like) use the thread exit value to indicate success:

#include <pthread.h>
#include <stdio.h>

int something_worked(void) {
    /* thread operation might fail, so here's a silly example */
    void *p = malloc(10);
    free(p);
    return p ? 1 : 0;
}

void *myThread(void *result)
{
   if (something_worked()) {
       *((int*)result) = 42;
       pthread_exit(result);
   } else {
       pthread_exit(0);
   }
}

int main()
{
   pthread_t tid;
   void *status = 0;
   int result;

   pthread_create(&tid, NULL, myThread, &result);
   pthread_join(tid, &status);

   if (status != 0) {
       printf("%d\n",result);
   } else {
       printf("thread failed\n");
   }

   return 0;
}

If you absolutely have to use the thread exit value for a structure, then you'll have to dynamically allocate it (and make sure that whoever joins the thread frees it). That's not ideal, though.

Steve Jessop
+2  A: 

You are returning a reference to ret which is a variable on the stack.

Maurits Rijk
+3  A: 

You are returning the address of a local variable, which no longer exists when the thread function exits. In any case, why call pthread_exit? why not simply return a value from the thread function?

void *myThread()
{
   return (void *) 42;
}

and then in main:

printf("%d\n",(int)status);   

If you need to return a complicated value such a structure, it's probably easiest to allocate it dynamically via malloc() and return a pointer. Of course, the code that initiated the thread will then be responsible for freeing the memory.

anon
+1 Don't use the heap to store an int, please
jbcreix
If you actually mean "why not", then the reason why not is that casting 42 to a pointer type is undefined behaviour. Of course if it does anything bad then it's one of those "it's a C implementation, Jim, but not as we know it" moments.
Steve Jessop
@Steve There are times when I will risk a little UB - this is one of them - the alternatives are so messy.
anon
Me too, but I wouldn't put the "portable anywhere" sticker on the box.
Steve Jessop
This works but I dont understand how. What does the cast (void*) mean? I am making a pointer from the number 42 and then casting it back to int? Does it have any drawbacks? Finally what is the pthread_exit() good for when I can use just return?
Petr Peller
anon
`pthread_exit` is like `exit`. It allows your thread to bail out early in the same way that a program can bail out early, and it can be called from any code in the thread, whereas to return you have to make your way back up to the thread entry point. The difference, of course, is that in a thread you'll most likely leak resources all over the place if you try to exit other than back out through the entry point.
Steve Jessop
I think I see the point now. Thanks for the answers!
Petr Peller
A: 

Question : What is the best practice of returning/storing variables of multiple threads? A global hash table?

This totally depends on what you want to return and how you would use it? If you want to return only status of the thread (say whether the thread completed what it intended to do) then just use pthread_exit or use a return statement to return the value from the thread function.

But, if you want some more information which will be used for further processing then you can use global data structure. But, in that case you need to handle concurrency issues by using appropriate synchronization primitives. Or you can allocate some dynamic memory (preferrably for the structure in which you want to store the data) and send it via pthread_exit and once the thread joins, you update it in another global structure. In this way only the one main thread will update the global structure and concurrency issues are resolved. But, you need to make sure to free all the memory allocated by different threads.

Jay