Reputation: 3839
So I'm reading about synchronization in .NET and a curiosity hit me. When you have multiple objects READING on the same data you should use ReaderWriterLock.AcquireReaderLock
to ensure that no other object modifies the data while there's a lock in it.
Now... If I'm sure that no object will modify the data (while it's being read at least) does it make sense to acquire a ReaderLock
? Or is it advised to use and why?
Upvotes: 1
Views: 1020
Reputation:
Theoretically, if you're 100% positive that nothing is going to modify the data while your readers are reading the data, then no, it's not necessary to get a reader-lock. A reader-lock is used to prevent writers from modifying the data while it's being read, but still allows multiple readers to access that data concurrently.
According to the MSDN documentation:
ReaderWriterLock is used to synchronize access to a resource. At any given time, it allows either concurrent read access for multiple threads, or write access for a single thread.
Upvotes: 2
Reputation: 1614
If you are sure no one is mutating the data no locks are needed. However, the key is that absolutely no shared data is being modified. For example, if you are reading data from an instance that has lazily initialized internal state you may run into potential race conditions.
Upvotes: 3