public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
From: Jason Nye <jnye@nbnet.nb.ca>
To: 'Pthreads-win32' <pthreads-win32@sourceware.cygnus.com>
Subject: asynchronous cancellation
Date: Fri, 12 Nov 1999 18:08:00 -0000	[thread overview]
Message-ID: <382CCAEB.ABCCED24@nbnet.nb.ca> (raw)

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

             reply	other threads:[~1999-11-12 18:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-11-12 18:08 Jason Nye [this message]
1999-11-16 19:17 ` Ross Johnson
1999-11-17  4:46   ` Jason Nye
1999-11-15  7:45 Bossom, John
1999-11-16  1:30 ` Ross Johnson
1999-11-16 13:11   ` Jason Nye
1999-11-16 14:40 Bossom, John

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=382CCAEB.ABCCED24@nbnet.nb.ca \
    --to=jnye@nbnet.nb.ca \
    --cc=pthreads-win32@sourceware.cygnus.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).