c# - Synchronization over objects in a dictionary -


i have dictionary maintain in c# has string class object mapping.

public class parent {               public dictionary<string, valueclass> clienttofilesystemmap {get;set;} }  class valueclass {     //some internal state      valueclass createclone()     {         create clone of object , return     }      void update()     {         change state      } } 

now there can simultaneous threads can updating , cloning same object @ same time. want synchronize access clone not return half updated object.

one way found create private lock object in class valueclass , acquire lock before operation. other option use [methodimpl(methodimploptions.synchronized)] mentioned here - c# version of java's synchronized keyword?. option create dictionary of objects similar class objects , take lock on them.

what best way this?

you can use concurrentdictionary control access dictionary elements - if sounds need private lock per object - once thread has reference - dictionary isn't issue.

private object privatelock = new object();  void update(){     lock(privatelock)     {         //change state      } }  valueclass createclone(){     lock(privatelock)     {         //clone      } } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -