Subj : Re: returning arguments To : comp.programming.threads From : loic-dev Date : Mon Jun 27 2005 09:51 am Salut Huub, > I know this isn't compilable code, but it's just to give an idea. My > question(s): > - Do threads *have* to be void? >From a pure semantic point of view, thread should be function returning a void*, that is a pointer to something. > - Can I pass on return values directly from the thread? Yes, this is possible if the returned type can pad on a void*, that is its size is <= sizeof(void*). For instance to return an integer, assuming that sizeof(int)<=sizeof(void*), then you can proceed as follows: void* thread (void* args) { int result; .... return (void*) result; } and where you're joining the thread: void *retp; int retv; .... rc = pthread_join (thread_id, &retp); assert (rc==0); retv = (int) retp; > - If not, how do I pass them on? If you need to return a type that is bigger than a void*, you can proceed as follows: In the thread: - allocate the type on the heap using e.g. malloc - return a pointer to the type located on the heap void* thread (void* args) { void* retp; .... retp = malloc (sizeof(myType); // fills-up (*retp) .... return retp; } where you're joining the thread: - reads the type - free the heap void *retp; .... rc = pthread_join (thread_id, &retp); // read *(retp) .... free (retp); HTH, Loic. .