From: William Kempf [mailto:williamkempf@hotmail.com]
Sent: 11 August 2002 23:31
> >From: "Victor A. Wagner, Jr." <vawjr@rudbek.com>
> >Date: Sun, 11 Aug 2002 11:59:15 -0700
> >
> >At Sunday 2002/08/11 06:48, you wrote:
> >>>From: "Victor A. Wagner, Jr." <vawjr@rudbek.com>
> >>>At Thursday 2002/08/08 07:49, you wrote:
> >>>>----- Original Message -----
> >>>>From: "Victor A. Wagner, Jr." <vawjr@rudbek.com>
> >>void my_thread(boost::thread* pthread)
> >>{
> >>   try
> >>   {
> >>      pthread->join();
> >>   }
> >>   catch(...)
> >>   {
> >>      cout << "main() threw an exception" << endl;
> >>   }
> >>   delete pthread;
> >>}
> >>
> >>int main()
> >>{
> >>   boost::thread* main = new thread();
> >>   boost::thread thrd(boost::bind(&my_thread, main));
> >>   thread_exit(); // Yes, this is something that needs added to
> >>                  // Boost.Threads, and will be.
> >>   throw "error!";
> >>}
> >>
> >>The above can't be done portably, AFAIK, and is likely to result in
> >>termination instead of the expected passage in join().
> >
> >I have no idea what this is supposed to "do" so that it's
> not portable
> >seems irrelevant.
> >Although I have no idea what thread_exit() will actually do,
> having it
> >execute the next line of code seems counter-intuitive.
> >The following code works as I expected on VC++.NET (not that
> I'm attempting
> >to hold it out as either definitive nor conforming) and if
> the "if (argc >
> >2)" is removed or changed to, say, >1, the "unexpected
> exception" occurs,
> >otherwise this executes to completion without "problem".
>
> Shoot.  My apologies.  I wrote that way too fast.  The throw
> should have
> occured before the call to thread_exit().  The point being
> that, as per
> behavior on every platform I'm familiar, thread_exit() (i.e.
> its platform
> equivalent) causes the thread to exit, and in the case of the
> main thread
> this means the process doesn't terminate until all other
> threads complete. 
> The throw skips the call to thread_exit(), and thus the
> terminate() is
> called because of the uncaught exception.  This means the main thread
> behaves differently from all other threads with regard to exception
> handling... unless you 1) can change the language rules or 2)
> make the
> exception handling consistent by having threads call
> terminate() in case
> there's an uncaught exception as well.

Alternatively, you could just set a terminate_handler which calls thread_exit, and ensure that uncaught exceptions in all threads terminate that thread, and only that thread. This is then consistent across all threads. As I posted before, it works successfully on MSVC/win32threads MSVC/pthreads, gcc-mingw/win32threads gcc-mingw/pthreads gcc-sparc-solaris/pthreads.

If join is called on a thread that terminated through an exception, then you could notify the caller of join that the thread terminated through an uncaught exception, but that is another issue.

Anthony