[LC++]Question on pthreads

Krishna Monian k_tutorials at yahoo.com
Thu Jul 15 06:28:02 UTC 2004


I still have a problem though. After creating the
thread in my main function, this is part of the code
that follows.

while(getchar() != '\n') {}

So basically I am waiting till the user hits the enter
key. As soon as the user does this, my program will
cleanup and quit. The thread that I spawn also does
some sort of polling operation.

If I use pthread_join in main() then the program
doesn't terminate when the user hits return (obviously
it won't, since it has joined the other thread).

How do I go about achieving this?

Thanks
Krishna


--- Jack Lloyd <lloyd at acm.jhu.edu> wrote:
> 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. :)
> 
> -Jack
> 
> On Wed, Jul 14, 2004 at 02:11:06PM -0700, Krishna
> Monian wrote:
> > Hi, 
> > 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?
> > 
> > Thanks
> > Krishna
> > 
> > 
> > 		
> > __________________________________
> > Do you Yahoo!?
> > New and Improved Yahoo! Mail - Send 10MB messages!
> > http://promotions.yahoo.com/new_mail 
> > _______________________________________________
> > This is the Linux C++ Programming List
> > :
> http://lists.linux.org.au/listinfo/tuxcpprogramming
> List
> _______________________________________________
> This is the Linux C++ Programming List
> :
> http://lists.linux.org.au/listinfo/tuxcpprogramming
> List
> 



		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
http://promotions.yahoo.com/new_mail 



More information about the tuxCPProgramming mailing list