In a FileWriter
class that will be used by different threads, I currently use a ReaderWriterLockSlim
to prevent errors occurring when two threads try to write to the file at the same time like this:
(1)
public class FileWriter
{
private ReaderWriterLockSlim readerWriterLock = new ReaderWriterLockSlim();
public void WriteToFile(string message)
{
try
{
this.readerWriterLock.EnterWriteLock();
// the writing happens here
}
finally
{
this.readerWriterLockSlim.ExitWriteLock();
}
}
}
which does work. But after that, I read that ReaderWriterLockSlim
implements IDisposable
, and so I wondered whether
(2)
public class FileWriter
{
public void WriteToFile(string message)
{
using (ReaderWriterLockSlim readerWriterLockSlim = new ReaderWriterLockSlim())
{
readerWriterLockSlim.EnterWriteLock();
// the writing happens here
readerWriterLockSlim.ExitWriteLock();
}
}
}
would be the "better" approach and whether it might introduce some new drawbacks. My gut feeling tells me that I probably shouldn't create a new ReaderWriterLockSlim
every time the method is called but rather only once as in (2)
.
Sadly though, it does not work (it's as if I hadn't even used a lock) and so I figured that (2)
can't be correct.
But then again, why would ReaderWriterLockSlim
implement IDisposable
if it wasn't planned to be used as in (2)
?
What is the correct usage of ReaderWriterLockSlim
?