[LC++]Question on pthreads
lloyd at acm.jhu.edu
Thu Jul 15 05:23:02 UTC 2004
Yes. If the function created by pthread_create returns a value, then when you
call pthread_join, it will return this status to you. In this way you can
easily exit the thread and return a status code signaling the rest of your
program to abend. That's why your thread start routines must return a void*,
that's the status code.
If you want to return an integer or some such, remember to dynamically allocate
it and return the pointer -- if you return &status_code, it will go away once
the thread exits. Obvious, but sometimes it's easy to forget (especially with
pthreads). And, of course, free it in your main thread after you pthread_join
the other thread.
Since this when to the C++ list and not the C list, I'll also remind you that
you can't (safely) throw an exception in a thread and have it be caught outside
that thread. It could be done, but nobody requires it, and I'm sure it would be
very difficult to implement, so nobody does. Just in case you were thinking
about doing that to signal errors. :)
On Wed, Jul 14, 2004 at 02:11:06PM -0700, Krishna Monian wrote:
> I have a pthread that is spawned from my main function
> and then the main function just waits until the user
> hits enter. Now I want the program to terminate if a
> something unexpected in the thread spawned. How
> exactly do I go about doing this?
> Even better would be if the thread could return a
> value to the main function. Is this possible?
> Do you Yahoo!?
> New and Improved Yahoo! Mail - Send 10MB messages!
> This is the Linux C++ Programming List
> : http://lists.linux.org.au/listinfo/tuxcpprogramming List
More information about the tuxCPProgramming