What I want to be able to do is to access a List
on multiple threads. This is thread safe, so I can just do it without worries. The only problem is that occasionally, I must modify the List
. So, I want to prevent other threads from using the List
only when it is being modified.
This is what I'm thinking, is there a better way?
volatile bool isReading = false;
volatile bool isWriting = false;
object o = new object();
public void StartRead()
{
lock (o)
{
while (isWriting || isReading) ;
isReading = true;
}
}
public void StopRead()
{
isReading = false;
}
public void StartWrite()
{
lock (o)
{
while (isReading) ;
isWriting = true;
}
}
public void StopWrite()
{
isWriting = false;
}