From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ross Johnson To: "'Pthreads-win32'" Subject: Re: asynchronous cancellation Date: Tue, 16 Nov 1999 19:17:00 -0000 Message-id: References: <382CCAEB.ABCCED24@nbnet.nb.ca> X-SW-Source: 1999/msg00135.html John, Jason, all, The following applies to the C++ version of things, but SEH versions should hopefully be similar. Following on from John's explanation of C++ stack unwinding etc, and the fact that pthreads-win32 implements cancelation by throwing an exception, and the fact that the C++ version of pthread_cleanup_pop is implemented as a destructor (after John's original implementation): For deferred cancelation: 1) stack unwinding should occur properly IMO 2) cleanup handlers should be called. For async cancelation: 1) Jason's method could be used with the following simple change to AsyncCancelPoint to work with the current pthreads-win32 mechanism: void cancelThread(HANDLE hThread) { ::SuspendThread(hThread); if (::WaitForSingleObject(hThread, 0) != WAIT_TIMEDOUT) { // Ok, thread did not exit before we got to it. CONTEXT context; context.ContextFlags = CONTEXT_CONTROL; ::GetThreadContext(hThread, &context); // _x86 only!!! context.Eip = (DWORD)AsyncCancelPoint; ::SetThreadContext(hThread, &context); ::ResumeThread(hThread); } } // declare AsyncCancelPoint: void AsyncCancelPoint() { // This is all it needs to do to call cleanup handlers, // call object destructors, and exit the thread. throw Pthread_exception_cancel(); } For cancelation in general: 1) the following macro should be included in pthread.h: #define catch(E) \ catch(Pthread_exception_cancel) { \ throw(); \ } \ catch(E) Is this getting closer to the ideal, or am I way off the track? Ross +----------------------+---+ | Ross Johnson | | E-Mail: rpj@ise.canberra.edu.au | Info Sciences and Eng|___| | University of Canberra | FAX: +61 6 2015227 | PO Box 1 | | Belconnen ACT 2616 | WWW: http://willow.canberra.edu.au/~rpj/ | AUSTRALIA | +--------------------------+