pthread_cancel and EH: let's try this again
Alexander Terekhov
terekhov at web.de
Wed Jul 20 11:35:25 UTC 2005
Peter Dimov wrote:
[...]
> A cancellation request is a request for a thread to not progress any
> further. It's pretty much a textbook example for using exceptions - a
> long-distance return all the way up, with the middle layers nearly never
> needing to not propagate it.
Yup. The effect of cancel delivery is (ought to be) nothing but
void attempt_deliver_cancel() {
int canceltype;
// in the case of async cancel delivery
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &canceltype);
// return all the way up (if cancel is expected)
pthread_exit(PTHREAD_CANCELED);
// unexpected
pthread_setcanceltype(canceltype, &canceltype);
}
And in details...
#define PTHREAD_CANCELED std::thread_canceled()
struct thread_canceled {
operator void * () { return &unique; }
static thread_canceled unique;
};
class thread_termination_request : public std::exception ...
class thread_cancel_request : public std::thread_termination_request ...
class thread_exit_request : public std::thread_termination_request ...
template<typename T> class thread_exit_value : public
std::thread_exit_request ...
extern "C" void pthread_exit(void * ptr)
throw(std::thread_termination_request) {
ptr == PTHREAD_CANCELED ? std::thread_cancel() :
std::thread_exit(ptr);
}
template<typename T>
void thread_exit(T value) {
int cancelstate;
assert(std::thread_self().can_exit_with<T>());
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate);
throw thread_exit_value(value);
}
template<>
void thread_exit(std::thread_canceled) {
thread_cancel();
}
void thread_cancel() {
int cancelstate;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate);
assert(cancelstate == PTHREAD_CANCEL_ENABLE)
throw_if_expected std::thread_cancel_request();
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancelstate);
}
Or something like that. ;-)
regards,
alexander.
More information about the c++-pthreads
mailing list