Re: ReaderWriterLockSlim + Dispose?
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Fri, 04 Apr 2008 17:11:44 -0700
On Fri, 04 Apr 2008 16:33:09 -0700, NvrBst <nvrbst@xxxxxxxxx> wrote:
One last question today :) Most locks I've used never had a Dispose
(Monitor, ReaderWriterLock, etc). Looking at the
ReaderWriterLockSlim's Dispose method its just cleaning up some
Threading.WaitHandle's.
Why do you write "just"? I would guess that for most classes that implement IDisposable, they "just" clean up some things. Are you implying that the ReaderWriterLockSlim.Dispose() is less important than for other classes that implement IDisposable?
Whenever I see a Dispose method I always want to call it after I'm
done with the object.
Yes.
All examples I've ever found online don't use
dispose with the ReaderWriterLockSlim.
Do you mean that the examples use ReaderWriterLockSlim but don't dispose it when the code is done with it? That's a bug.
Making my classes that use
this lock IDisposable for this reasion alone is a little frustrating,
and makes the implementation a little harder (since I have to
constantly check to see if the locks disposed, mainly when using it
with a thread pool).
I don't understand why it's a problem. Why do you have to check to see if the lock is disposed? Why would it be disposed unless you're sure you're done with it (i.e. your own Dispose() method has been called)?
Since only WaitHandles are getting closed, the GC will eventually
clean them if I ignore the Disposing on the ReaderWriterLockSlim?
Not necessarily. If the finalizer thread gets around to running, they will eventually get cleaned up, yes. But there's no guarantee that this will ever happen, or when it will happen. You should not design code that relies on that (other than implementing your own finalizer, of course).
Is
there performance increases in using the disposable WaitHandles vs how
other locks without Dispose() do it?
It seems to me that the main difference is one of functionality. ReaderWriterLockSlim serves a very specific purpose. If you have need for that purpose, you are likely going to need some disposable synchronization object anyway, so using a lock implementation instead that itself needs disposing doesn't make things any worse with respect to disposal, but of course simplifies your life in that you don't have to reimplement all the stuff in ReaderWriterLockSlim.
If you don't need the added functionality that ReaderWriterLockSlim provides, then you might as well use one of the simpler, non-disposable synchronization techniques. Alternatively, you could use the older ReaderWriterLock class, and accept the additional performance overhead it has.
Pete
.
- Follow-Ups:
- Re: ReaderWriterLockSlim + Dispose?
- From: NvrBst
- Re: ReaderWriterLockSlim + Dispose?
- References:
- ReaderWriterLockSlim + Dispose?
- From: NvrBst
- ReaderWriterLockSlim + Dispose?
- Prev by Date: Re: System.Timers.Timer Issue
- Next by Date: Re: Urgent:How to Check availability of web service
- Previous by thread: ReaderWriterLockSlim + Dispose?
- Next by thread: Re: ReaderWriterLockSlim + Dispose?
- Index(es):
Relevant Pages
|
Loading