Re: thread.Abort()
From: Jeffrey Wynn (sonofsheaf_at_optonline.net)
Date: 02/16/04
- Next message: winthux: "Re: [newbie] conversion between types"
- Previous message: Vadym Stetsyak: "Re: Online C# tests"
- In reply to: Dave: "Re: thread.Abort()"
- Next in thread: Dave: "Re: thread.Abort()"
- Reply: Dave: "Re: thread.Abort()"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 16 Feb 2004 09:07:23 -0500
Here is a link to some MSDN documentation that explains some of the
threading concepts that were referenced in this thread and how they are
dealt with in the .NET Framework.
Hope this helps.
-- Jeffrey Wynn sonofsheaf@nospam.net "Dave" <noSpamdlevineNNTP2@wi.rr.com> wrote in message news:e7iHa0C9DHA.1804@TK2MSFTNGP12.phx.gbl... > > "Daisy" <daisy@nospam.oops> wrote in message > news:c0oi8u$ra1$1@linux01.dannytuppeny.com... > > "Dave" <noSpamdlevineNNTP2@wi.rr.com> wrote in message > > news:ekMEIo$8DHA.3364@TK2MSFTNGP09.phx.gbl... > > <snip> > > > You are correct; there are many code snippets that ignore race > conditions. > > > Writing correct multithreaded code is a very difficult task and even > > > thoroughly-tested production code can have threading bugs lurking in it. > > > There are also many people who think they know the issues but really > > don't. > > > > Righto. But how *would* you overcome something like this? Say you wanted > to > > read a property from an object you're not sure still exists. You test it > > exists first, but you've still got a race condition. Wrapping it in a try > > block would work, but is that the best way? > > -- > > If you have a reference to an object then the object will be around so long > as you hang onto the reference. The issue is not whether the memory is still > valid or not - as long as the reference is rooted the memory is still > valid - the original issue is one of race conditions between threads. > > A try block does not help prevent race conditions, it only catches an > exception that is thrown in the guarded block of code. The only way to > prevent race conditions is to use synchronization mechanisms; the code > snippet shown does absolutely nothing in this regard. > > Further, the entire premise of the code snippet shown is incorrect. There > are many side-effects to inducing an abort exception asynchronously in > another thread, all of them bad. For example, the target thread may be > executing code in a finally block - this can be interrupted with unknown > consequences. Another is that if the exception occurs while the target > thread is executing code in a static constructor the type is rendered > unusable in that appdomain. > > In addition thread objects continue to exist for some time even after the > thread is no longer "alive" (holding a reference to the thread will do > this), and if you call abort on a thread that is already terminated the > request is ignored, so I still am mystified as to why the author of that > article thought it necessary to call IsAlive prior to calling Abort. > > In the snippet... > > if ( t.IsAlive ) > t.Abort(); > > The amount of time that can elapse after calling IsAlive and before > Thread.Abort is executed is unbounded. The call to IsAlive can indicate the > target thread is alive but before the next line of code executes a context > switch can occur, other code executes, and the target thread could actually > die. Even if the thread is still alive the amount of time it takes for the > thread to actually terminate is also unbounded. > > One safe way for one thread to signal another thread to terminate is to use > a signalling mechanism, such as a ManualResetEvent, that the calling thread > sets to the signalled state and the target threads periodically checks. This > is a synchronous mechanism that allows the target thread to reach a known > safe point in its execution path before it terminates. This is a relatively > standard technique. If you need to know when the thread has terminated you > can wait on the thread object to become signalled (do a Join on the thread > object). > > Dave > > > >
- Next message: winthux: "Re: [newbie] conversion between types"
- Previous message: Vadym Stetsyak: "Re: Online C# tests"
- In reply to: Dave: "Re: thread.Abort()"
- Next in thread: Dave: "Re: thread.Abort()"
- Reply: Dave: "Re: thread.Abort()"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|