Does the cancelation exception have a name?

Peter Dimov pdimov at mmltd.net
Wed Nov 1 19:18:46 UTC 2006


Under the current NPTL implementation, can the cancelation exception be 
caught by name (and then rethrown)? If not, are there problems (technical or 
otherwise) with giving it a name so it can be caught?

Motivating example 1:

extern "C" int f() // C API, cancelation point
{
    try
    {
        cpp_implementation();
        return 0; // success
    }
    catch( cancel_exception )
    {
        throw;
    }
    catch( bad_alloc )
    {
        return ENOMEM;
    }
    catch( ... )
    {
        return -1;
    }
}

This is a function with a C interface and a C++ implementation. It 
translates all C++ exceptions into error codes, but the cancelation 
exception is special and needs to be rethrown, or else.

Motivating example 2:

void threadproc()
{
    try
    {
        store_result( user_function() );
    }
    catch( cancel_exception )
    {
        // record that the user task has been canceled
        throw;
    }
    catch( ... )
    {
        // record that the task terminated with an exception
    }
}

Here a user task is executed in a thread, and its result (which can be of an 
arbitrary type) is stored for later use. This enables usage of the form:

future<int> f = call_in_thread( user_function );

// do something to exploit parallelism

int r = f.get();

If the cancelation exception is and will remain unnamed, is it possible to 
provide a way to detect, in a catch( ... ) clause, whether the current 
exception is a cancelation exception? Something like

bool is_cancel_exception();

could also allow the two use patterns above.

Thanks in advance for any replies or comments. 




More information about the c++-pthreads mailing list