This is the mail archive of the
pthreads-win32@sourceware.cygnus.com
mailing list for the pthreas-win32 project.
RE: asynchronous cancellation
- To: 'Jason Nye' <jnye at nbnet dot nb dot ca>, 'Pthreads-win32' <pthreads-win32 at sourceware dot cygnus dot com>
- Subject: RE: asynchronous cancellation
- From: "Bossom, John" <John dot Bossom at Cognos dot COM>
- Date: Mon, 15 Nov 1999 10:44:58 -0500
Interesting solution...
One little problem though... this solution makes use of _declspec( thread ).
Use of this functionality effectively prevents the use of any
dynamically loaded DLLs that want to use the threading library.
(memory for variables declared as declspec( thread ) is only accounted
for at initial process load time; any declarations within dynamically
loaded DLLs are missed - you end up with unpredictable behaviour).
To work with the pthreads-win32 library, your AsyncCancelPoint
needs to get pSelf from dynamically declared TLS... (i.e. pthread_self,
or it's internal implementation).
Just a note to C++ users: one shouldn't use the built-in pthreads
cancellation if they expect C++ stack unwinding to work....
Thanks for your contribution!
John.
-----Original Message-----
From: Jason Nye [mailto:jnye@nbnet.nb.ca]
Sent: Friday, November 12, 1999 9:20 PM
To: 'Pthreads-win32'
Subject: asynchronous cancellation
Hi, all
I've noticed a lot of you discussing asynchronous cancellation and
mentioning how difficult it is to do under win32. I did some research
and found a bullet-proof way of doing it (from a J. Richter example).
Here is a sample of the code I used in my library:
If thread x wants to cancel thread y asynchronously, thread x should
call cancelThread(y):
----------------------------------------------------------------------------
-----------------------
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()
{
// pSelf is a pointer to a ThreadInfo (each thread has one --
declared as __declspec(thread)).
popCancelCleanupHandlers(pSelf);
callDestructors(pSelf);
_endthreadex(PTHREAD_CANCELLED);
}
----------------------------------------------------------------------------
-----------------------
That is it. If a thread's cancel state is asynchronous and another
thread requests that it be cancelled, the thread will suddenly find
itself executing AsyncCancelPoint which is exactly what you want.
If you want to see it in action in my C++ library, go to
http://www3.nbnet.nb.ca/jnye, follow the "current software projects"
link and download the latest version of ObjectThread. You'll see how it
fits into a complete library.
Hopefully this is useful,
Jason