Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-08-14 15:18:15


From: "David Abrahams" <dave_at_[hidden]>
> >
> > Well, I am not a compiler writer, but it seems to me that to implement
> > "throw;" and "catch", the compiler already needs a way to copy the
> > exception, complete with its original type. :-)
>
> Of course, but there's a lot more to it than that!
> How would you use the existing constructs to propagate an exception across
> threads? Show me the code that should have the semantics you'd like to
see.
> In particular, please show how the exception arrives at its destination.

I can't really answer these questions because I'm not sure of the meaning
behind "existing constructs", "propagate an exception across threads",
"arrives at its destination."

What we need:

int f()
{
    throw 5; // #1
}

int main()
{
    thread t(f);

    try
    {
        std::cout << t.join() << std::endl; // #2
    }
    catch(int x)

    {
        std::cout << x << std::endl;
    }
}

Now, at #1, the compiler needs to make a copy of '5' and store it somewhere
where the stack unwinding doesn't destroy it, right? Threads can see the
whole process memory (visibility issues notwithstanding), so at #2, t.join()
can execute the equivalent of "throw;" using the stored exception. There are
two threads, but only one C++ program, so the catch clause will be able to
handle the exception.

A library solution needs to use dynamic allocation and clone() to preserve
the exception, and a virtual throw_this() to execute the rethrow, but a
compiler should be able to do better than that.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk