From: Udi Ben Senior on
Hi all,
Can anyone explain what happens when I interrupt a managed thread that
is executing an unmanged function?
Does the interrupt occur only after the thread has returned from the
unmanged call?

Thanks,
Udi
From: Peter Duniho on
Udi Ben Senior wrote:
> Hi all,
> Can anyone explain what happens when I interrupt a managed thread that
> is executing an unmanged function?
> Does the interrupt occur only after the thread has returned from the
> unmanged call?

That is correct. The CLR will not interrupt the thread while it's in
native code.

You should try to avoid designing your code such that it interrupts
threads in the first place, so normally the exact specifics of what
might happen when you do should not matter. If for whatever reason this
isn't possible (e.g. you're forced to deal with some crappy, third-party
code that has an infinite loop bug in it or something), you'll have to
use the unmanaged API to terminate a thread that's stuck in unmanaged code.

Note that if you anticipate having to do that, it is best to isolate the
entire unmanaged code in its own unmanaged thread that you can terminate
without risking having to terminate a CLR thread.

Also, be very careful about dealing with data that has been exposed to
the thread that's being terminated. It can be very difficult or
impossible to guarantee that the data that the thread was working on is
in any sort of coherent state, if the thread is terminated abnormally.
(Ideally, the API doesn't involve any mutable data shared between
threads�pass some immutable data in, get some completely different
immutable data back out; then all that terminating the thread causes is
you fail to get the data back out).

Really though, don't terminate threads. Multithreaded code is hard
enough to get right even when the execution of the thread is 100%
deterministic. Terminating threads can introduce subtle, non-obvious
issues, depending on exactly what the code is doing at the time it was
terminated.

Pete
 | 
Pages: 1
Prev: reply
Next: debugging a service