I have a dictionary that I maintain in C# which has a string to a class object mapping.
public class parent
{
public Dictionary<string, valueclass> clientToFileSystemMap {get;set;}
}
class valueclass
{
//some internal state
valueclass createclone()
{
create clone of this object and return
}
void update()
{
change state
}
}
Now there can be simultaneous threads which can be updating and cloning the same object at the same time. I want to synchronize the access so that clone does not return a half updated object.
One way that I found was create a private lock object in the class valueclass and acquire that lock before that operation. The other option would be to use [MethodImpl(MethodImplOptions.Synchronized)]
as mentioned here - C# version of java's synchronized keyword?.
Another option could be to create a dictionary of objects similar to class objects and take lock on them.
What would be best way to do this?