Note that there are some explanatory texts on larger screens.

plurals
  1. POIDisposable and ReaderWriterLockSlim
    primarykey
    data
    text
    <p>I have a class <code>MyClass</code>. This class have a field: <code>public ReaderWriterLockSlim rw;</code> (public for an easier example code). Many threads can read data from <code>MyClass</code> using <code>rw.EnterReadLock</code> etc. </p> <p>Also I have implemented <code>IDisposable</code> interface: </p> <pre><code>private void Dispose (bool pDisposing) { { if (pDisposing) // release managed resources { if (rw != null) { rwLockSlim.Dispose (); rwLockSlim = null; } } //--- release unmanaged resources // some code... isDisposed = true; // ... } } </code></pre> <p>Like you see, problem is when one thread is using <code>MyClass</code> when second thread call <code>Dispose</code> on myClass object. I cannot dispose ReaderWriterLockSlim because it will crash my application. So should I just remove that lines which releases managed resources? Anyway that ReaderWriterLockSlim will be collected by GC in near future, right? (but is this class resource expensive?).<br> Maybe I should add some lock(syncObject) in Dispose metod or something? </p> <p>EDIT: also I deal with AllocHGlobal, so I need to wait, until all threads will stop reading/writing to <code>myClass</code>. </p> <p>Different point of view:</p> <pre><code>public MyClass : IDisposable { public void EnterReadLock (); // calls rwLockSlim.EnterReadLock, // if object is disposed throws Exception public void ExitReadLock (); // same as above public void Dispose (); // wait until all threads exit from locks, // frees unamanged resources, mark class as disposed } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload