From: David Harel (hareldvd_at_nonexisting.hamakor.org.il)
Date: Thu 06 Jan 2005 - 18:17:21 IST
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, is the stack frame of the exiting thread kept until
the caller/joiner receives retval? (like in processes, wait() clears
zombie)?
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. How can I save retval safely?
attached a small example that seem to show the retval issue. In the
example I intentionally start the threads synchronously so I hope the
next thread will reclaim memory of the terminated one. I save the return
values in an array. As I suspected, the reference in the array becomes
invalid and eventually I get segment violation. If I do anything wrong
here, please correct me.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define NUM_THREADS 8
void *PrintHello(void *threadid)
{
int *id_ptr, taskid;
id_ptr = (int *) threadid;
taskid = *id_ptr;
printf("Thread %d says hello\n", taskid);
pthread_exit(& taskid);
}
int main()
{
pthread_t threads[NUM_THREADS];
int rc, i, t[NUM_THREADS];
int * retval[NUM_THREADS];
for(i=0; i < NUM_THREADS; i++)
{
t[i] = i;
rc = pthread_create(&threads[i], NULL, PrintHello,
(void *) & t[i]);
pthread_join(threads[i], (void *) &retval[i]);
}
for(i=0; i < NUM_THREADS; i++)
{
printf("thread returned value %d\n", * retval[i]);
}
sleep(1);
exit(0);
}
--
Thanks.
David Harel,
==================================
Home office +972 4 6921986
Fax: +972 4 6921986
Cellular: +972 54 4534502
Snail Mail: Amuka
D.N Merom Hagalil
13802
Israel
Email: hareldvd_at_ergolight-sw.com
=================================================================
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 : Thu 06 Jan 2005 - 18:35:06 IST