Is someList.Count
thread safe in C#?
Performance is very important to me and I can not use other thread safe collections due to the complexity of the main program
I know there can be many other cases, but just focus on this simple question:
Is _lock.EnterReadLock
necessary for someList.Count
or not because it might be an atomic attribute?
private ReaderWriterLockSlim _lock;
private List<SomeObject> _someList;
public void Add(SomeObject obj)
{
try
{
_lock.EnterReadLock();
if (_someList.Count < 10)
{
try
{
_lock.EnterWriteLock();
_someList.Add(obj);
}
finally
{
_lock.ExitWriteLock();
}
}
}
finally
{
_lock.ExitReadLock();
}
}