Re: ReaderWriterLockSlim + Dispose?



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
.



Relevant Pages

  • Re: ReaderWriterLockSlim + Dispose?
    ... was before the lock when it disposed, then it wouldn't result in any ... if you implement Dispose() that it be valid to call Disposemultiple ... I'm not too sure about this statment... ... In my opinion, it's possible to, clean ...
    (microsoft.public.dotnet.languages.csharp)
  • ReaderWriterLockSlim + Dispose?
    ... One last question today:) Most locks I've used never had a Dispose ... Whenever I see a Dispose method I always want to call it after I'm ... clean them if I ignore the Disposing on the ReaderWriterLockSlim? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Oldest item in your freezer??? (Gagger warning)
    ... my brother was helping her clean out her basement and he came across a freezer that had gotten unplugged years back, and my grandmother never bothered to clean it out. ... It was full of evil goo, and when he called the city to find out how to dispose of it, they suggested he wear a hazmat suit of sorts, and not to open it ndoors again. ... The basement is also home to six inches or so of compacted stuff that is near-permanently affixed to the floor from numerous floods. ... She takes a water exercise class at the local JCC, and takes home leftovers from ...
    (rec.food.cooking)
  • Re: garbage prophecy
    ... therefore, of the entire universe, of which we must be but an ... could help by keeping it clean. ... because the fees to dispose of these items were causing people to find ... we just have to make sure the locals know they can dispose of this ...
    (alt.gathering.rainbow)
  • Re: ReaderWriterLockSlim + Dispose?
    ... The thread won't "clean up as intended". ... Yes all "fast clean" dispose objects need to ... It does if you want to maintain it as a workable design philosophy (and even then, it's still higher maintenance than the correct way to do it). ...
    (microsoft.public.dotnet.languages.csharp)

Loading