Re: ReaderWriterLockSlim + Dispose?
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Sat, 05 Apr 2008 14:46:07 -0700
On Sat, 05 Apr 2008 11:17:01 -0700, NvrBst <nvrbst@xxxxxxxxx> wrote:
[...]In that particular example, you either need to decide whether you're going
to interrupt the threads that need the lock, or you're going to delay
cleanup of the lock until the threads are all done. You can't let the
threads run _and_ clean up the lock right away.
Aye. I understand this design wise, but, in the case of the thread
pool, you can't interrupt it.
Of course you can. Not that I was suggesting you use Thread.Abort(), but even that should work okay (exceptions are eaten by the thread pool, but your own code is aborted...the thread pool will just create a new thread later if needed). In reality, if you need a thread (thread pool or otherwise) to be interruptible, you just design it to be so. Use a volatile flag, have the code in the thread check the flag and exit when it can. Or any of a number of other signaling mechanisms that are available.
And waiting around for the threads to
finish isn't always a perferred solution;
If not, then you need to design the thread to be interruptible. That's not a problem.
maybe the locks were there
to syncronize actions (IE in the case of a chat program, the thread
could lock the send/recive method on a line basis, so if you stop
locking the worst that could happen would be something like Msg1:
"hello jim." Msg2: "test", resulting in "hetleslo jim .\nt".
If I were using a messaging application that produced a result like that, I would never ever run it again. At the very least, it suggests that the provider of that application was willing to let less-than-perfect behavior occur just to make their own life simpler. And worst, it's a strong clue that the provider may have underestimated the need to produce correct code.
Synchronization exists to ensure correct, predictable behavior from the code. If the code isn't synchronized and is allowing output to be intermingled when that wasn't part of the user's intent, who knows what else may have been overlooked by the provider?
If your
cleaning up, your probably not caring what else the threads going to
do as long as it doesn't crash, etc.
Well, first of all...the "as long as it doesn't crash" is an important consideration: how can you prove that your code won't crash if you've abandoned synchronization? Secondly, if you're cleaning up, then either it's okay to interrupt the threads or it's not. If it is, then you design those threads so that they can be interrupted and then you interrupt them. If it's not, then you have no choice but to wait for them to finish. What benefit is there in doing some of the cleanup early, and then still waiting on the threads? Especially when that cleanup removes objects that the threads need to use?
Pete
.
- Follow-Ups:
- Re: ReaderWriterLockSlim + Dispose?
- From: NvrBst
- Re: ReaderWriterLockSlim + Dispose?
- References:
- ReaderWriterLockSlim + Dispose?
- From: NvrBst
- Re: ReaderWriterLockSlim + Dispose?
- From: Peter Duniho
- Re: ReaderWriterLockSlim + Dispose?
- From: NvrBst
- Re: ReaderWriterLockSlim + Dispose?
- From: Peter Duniho
- Re: ReaderWriterLockSlim + Dispose?
- From: NvrBst
- ReaderWriterLockSlim + Dispose?
- Prev by Date: Re: asp webservice variables and persistence
- Next by Date: Re: Blocking calls in AsyncCallback?
- Previous by thread: Re: ReaderWriterLockSlim + Dispose?
- Next by thread: Re: ReaderWriterLockSlim + Dispose?
- Index(es):
Relevant Pages
|
Loading