Subj : Re: trying to make own version of pthread_exit() To : comp.programming.threads From : steve Date : Tue Mar 22 2005 09:36 pm In article , JS wrote: >I am trying to make my own implementation of pthread_exit(). I have read: > >http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/man/pthread_exit.html > >To prevent confusion I call the funtions mythread instead of pthread. > > >When a thread calls mythread_exit() it should be terminated safely. But it >should also store a "termination status void pointer" that any joining >threads can access. Yes, and the value stored needs to be valid after the thread stack has been deleted. >When I create a new thread, start_function is called which then calls >mythread_exit with the pointer that it returns. Is this the right use of >pthread_exit function? I store the return value that start_function produces >in a void pointer k. The value to be returned can either be something int-like cast into a void *, or a pointer to something in the heap. A pointer to something on the stack won't work, because the stack will be going away. [ snip ] >void mythread_exit(void *retval){ > void * k; > k = retval; > > printf("%d",k); > >} Uh, that doesn't quite do enough if you want to be like pthread_exit(). pthread_exit() can be called from anywhere in the execution flow and will stop that execution flow. >void *start_function(void *arg){ > int x = 22; > int *ip; > ip = &x; > > mythread_exit(ip); If you're expecting to see something related to 22, you would be better off saying "mythread_exit((void *)22);". In this case, x goes out of scope before the return value is fetched. And your implementation of mythread_exit() above is printing the value of the pointer. >return ip; >} The return is redundant. If you're serious about implementing a thread environment, I would suggest thinking very carefully about the lifetime of various objects; it's nasty to debug stack slams in a multithreaded environment, and debugger support is difficult to write, too. -- Steve Watt KD6GGD PP-ASEL-IA ICBM: 121W 56' 57.8" / 37N 20' 14.9" Internet: steve @ Watt.COM Whois: SW32 Free time? There's no such thing. It just comes in varying prices... .