Hello i need to user writerreaderlock in my method. I want to know how use it correctly.
I got a dictionnary of ObjectA
public class ObjectA
{
public ReaderWriterLock RWL {get;set;}
public ObjectB obj {get;set;}
public ObjectA()
{
RWL = new ReaderWriterLock();
}
}
public class ObjectB
{
int TTL {get;set;}
string Value {get;set;}
}
In my method I user a dictionnary of ObjectA, the key is a Guid, so presume that when i call dict[guid] return always an instance of my ObjectA (for the exemple)
public foo()
{
ObjecA objA = dict[guid];
objA.RWL.AcquireReaderLock(500);
if(objA.obj.TTL<=0)
{
objA.obj.RWL.AcquireWriterLock(1000);
objA.obj.Value = DateTime.Now().ToString();
objA.obj.RWL.ReleaseWriterLock();
}else{
int ttl = objA.obj.TTL;
Interlocked.Decrement(ref ttl);
}
objA.RWL.ReleaseReaderLock();
}
I am relly not sure of my using of reader and writer over there, how did i need to use reader writer lock, with a conditional validation ?