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:
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));
if (ret == NULL) {
// ...
}
*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.