This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Destructors and Threads, perfect together?
- To: Zack Weinberg <zack at rabi dot columbia dot edu>
- Subject: Re: Destructors and Threads, perfect together?
- From: george at moberg dot com
- Date: Wed, 12 May 1999 19:18:09 -0400 (EDT)
- Cc: egcs at egcs dot cygnus dot com
Zack, you are THE MAN!
I owe you a beer! (or some suitable non-alcoholic beverage if you don't drink)
If you're ever in Philadelphia, let me know.
It's been quite a while trying to find this bug, and to get such a clear
confirmation in such a short time is amazing.
[deletia]
> No, they [destructors for objects on the stack when a thread is cancelled]
> will not be called. You could get them to be called
> by stacking a cleanup handler that throws an exception.
Life's hard, eh?
> You will
> then have to use -fasynchronous-exceptions. You will also have to
> have an exception handler at the bottom of the call stack that calls
> pthread_exit(PTHREAD_CANCELLED), because the library function that
> handles cancels will get unwound, so it won't do it for you.
Does -fasynchronous-exceptions work with -fnew-exceptions?
> The cleanup handler needs to be at the very bottom of the handler
> stack, so that the call stack gets unwound *after* all your pthreads
> cleanups are executed. Define an exception object that you use only
> for this. Use deferred cancellation mode.
OK.
> And if you do all these things, it still may not work, but that would
> be a bug in the library or the compiler.
Hope not.
> g++ could conceivably do all this automatically, which would be a good
> idea because it would then know where that exception could be thrown
> from (in deferred cancellation mode). It shouldn't be on by default
> though, or it would drag the thread library into unthreaded programs.
That's OK. I have a CThread class that I wrote in my program into which I can
place all this.
One could build this functionality into the pthreads library itself, I would
think, where it wouldn't have to be linked into a non-threaded C++ program. I
get the feeling that this should be somewhere in there or the compiler to save
people from severe debugging nightmares.
Either that or the man page for pthread_create() should talk about this subject
in merciless detail, with example code. Maybe if I implement it, I'll put the
code somewhere or send it to the appropriate person.
Any suggestions?
---
George T. Talbot
<george@moberg.com>