Subj : Re: returning arguments To : comp.programming.threads From : Huub Date : Tue Jun 28 2005 09:34 pm David Butenhof wrote: > Huub wrote: > >> Is the right way to do it? >> >> struct displaygegevens >> { >> int gasstatus; >> int rookstatus; >> int temperatuur; >> }; >> >> void *LeesSensoren(void *threadid) >> { >> ... >> try >> { >> ... >> displaygegevens = {gasstatus, rookstatus, temperatuur}; >> return (void*)displaygegevens; > > > Here you're casting your STRUCTURE (not a pointer) to "void*". That > won't work. But even if you changed it to "(void*)&displaygegevens", > you're still returning a pointer to a single statically allocated > structure. That MAY be OK if your application logic ensures that there > can never be more than one thread running "LeesSensoren" at a time. > Note, that doesn't just mean you can't have two threads running at the > same time... you can't start a new LeesSensoren thread until > pthread_join() has completed for the previous thread AND you've > extracted all the information out of the static structure. If you can't > be sure that guarantee will always hold, then you'd need synchronization > to protect your access. You'd be better off just using malloc() to have > each thread make its own copy of the structure, and return a pointer to > that copy. (Again, after extracting the data the joiner would simply > free() the structure.) > >> } >> catch(..) >> { >> } >> } >> >> void x() >> { >> try >> { >> int retv; >> void *retp; >> rc = pthread_create(...); >> pthread_exit(NULL); >> rc = pthread_join(.....); >> assert (rc == 0); >> return(retv = (int)retp); > > > You've copied too much and not enough of Loic's logic. You don't show > that you're actually extracting the returned value from pthread_join(), > which makes me a little nervous given the rest of your code. But > presuming you really understood that part, you're also casting retp (now > the ADDRESS of your structure) to an int for return from a void > function. (This wouldn't even compile.) You need to cast the returned > void* to a pointer to your structure type, and then read the members of > the structure. > I *thought* I understood it. Could you explain a bit more of the last part of your comment please, including the casting to a pointer to the structure? .