Interrput a thread

A

Adam Skutt

This is a case that .NET (C#) handles better than Python or Java.

Nope, read the documentation for Thread.Abort() carefully.
Thread.Abort() can cause termination inside a static constructor,
which is very bad. You sure your application can cope with that?
Mine can't. Thread.Abort() is only safe for self-abortion, app domain
termination and a few other very narrow and obscure scenarios. It is
not a safe way to end arbitrary threads doing arbitrary processing.
It's another place where people
sometimes have a genuine need/use case yet people will insist on
telling them they don't *really* want it...

Because there's no safe way to do it. It's fundamentally a racy
operation, with the typical horrible consequences when you lose.
Removing the race requires support from the application, i.e., you
have to write the code yourself. Frequently, it's simply not possible
to remove the race.

Adam
 
J

John Nagle

Nope, read the documentation for Thread.Abort() carefully.
Thread.Abort() can cause termination inside a static constructor,
which is very bad. You sure your application can cope with that?
Mine can't. Thread.Abort() is only safe for self-abortion, app domain
termination and a few other very narrow and obscure scenarios. It is
not a safe way to end arbitrary threads doing arbitrary processing.


Because there's no safe way to do it. It's fundamentally a racy
operation, with the typical horrible consequences when you lose.
Removing the race requires support from the application, i.e., you
have to write the code yourself. Frequently, it's simply not possible
to remove the race.

There are systems where there's support designed in for thread
abort. LISP/Scheme systems tend to support it. QNX, the real-time
OS, has well worked out thread-abort semantics at the C level.
(QNX has really good features for "not getting stuck", like the
ability to put a time limit on any system call.)
But Python doesn't support things like that.

What you'd really like in Python is the ability for one thread
to be able to force an exception in another thread, plus a
mechanism for locking out such exceptions for critical sections.
It's not worth having, though, in a system where you can really only
run one thread at a time.

John Nagle
 
A

Adam Skutt

     There are systems where there's support designed in for thread
abort.  LISP/Scheme systems tend to support it.  QNX, the real-time
OS, has well worked out thread-abort semantics at the C level.
(QNX has really good features for "not getting stuck", like the
ability to put a time limit on any system call.)

Yes, but "not getting stuck" and ending the thread execution is only
one small part of the problem (and arguably the least significant).
What we really want is a way to abort without harming other threads of
execution, which is the hard part. QNX doesn't ipso facto make that
easier. Functionality like time limits on system calls is more about
latency guarantees and priority than "getting stuck" in a deadlock
sense.
     What you'd really like in Python is the ability for one thread
to be able to force an exception in another thread, plus a
mechanism for locking out such exceptions for critical sections.
It's not worth having, though, in a system where you can really only
run one thread at a time.

Exceptions and critical sections are rather fundamentally
incompatible, hence the absurd amount of gymnastics .NET goes through
to attempt to make ThreadAbortException functional (and still fails
rather miserably). If you had STM or 'antitry' blocks, then
exceptions might be a semi-saneish way to abort a thread. Without
either, I'm not entirely convinced of the utility.

Only allowing the exception to be thrown from defined cancellation
points is much better (ala POSIX threads), but diminishes the utility
for things that are mostly grinding away in userspace.

Adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top