From: Cyril Scetbon (cyril.scetbon_at_nonexisting.hamakor.org.il)
Date: Fri 07 Jan 2005 - 00:39:36 IST
You can simply use id_ptr as follows:
void *PrintHello(void *threadid)
{
int *id_ptr, taskid;
id_ptr = (int *) threadid;
taskid = *id_ptr;
printf("Thread %d says hello\n", taskid);
pthread_exit(id_ptr);
}
David Harel wrote:
> Thanks for the info, It is very helpful.
>
> The man pages of pthread_exit() says prototype is:
> void pthread_exit(void *retval);
>
> In my stupidity I assumed I must return an address of...
> Sending the actual value casted to (void *) works well.
>
>
> Pablo 'merKur' Kohan wrote:
>
>> On Thu, 2005-01-06 at 18:17 +0200, David Harel wrote:
>>
>>
>>> Hi,
>>>
>>> I hope you can help me. I was looking for the right answer
>>> everywhere on the net.
>>>
>>> When I do pthread_exit(retval); and I do pthread_join(&retval); at
>>> the "calling" thread...
>>>
>>
>> [snip]
>>
>>
>>> Still, retval is a pointer, i guess to somewhere in the terminated
>>> thread space (stack frame) but after pthread_join() the terminated
>>> thread space can be reclaimed. What if another thread reclaims that
>>> space and reuse it. Does that mean that retval will be invalid? and
>>> if so.
>>>
>>
>> That's exactly your problem.
>> You return a pointer (&taskid) to an area you don't own anymore...
>> So in the best case, you'll get garbage, and when not assigned to one of
>> your threads, it will SEGV.
>>
>>
>>
>>> How can I save retval safely?
>>>
>>
>> To solve this you can (based on your example):
>> 1) return taskid instead of it's pointer (&taskid), or
>> 2) if you have to return a significant amount of memory, then have it
>> alloc'ed.
>>
>> Hope this helps,
>>
>>
>>
>
=================================================================
To unsubscribe, send mail to linux-il-request_at_linux.org.il with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail linux-il-request_at_linux.org.il
This archive was generated by hypermail 2.1.7 : Fri 07 Jan 2005 - 00:55:09 IST