0

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?

Community
  • 1
  • 1
Piyush
  • 321
  • 2
  • 13
  • Can you post a more specific code (in code you pasted, there is no dictionary that you're mentioning) for a better answer? – LB2 Mar 10 '14 at 15:10
  • Done. Although dictionary is not really important here. – Piyush Mar 10 '14 at 15:14

2 Answers2

2

You can use a ConcurrentDictionary to control access to the dictionary elements - but if sounds like you will need a private lock per object - as once a thread has a reference - the dictionary isn't really the issue.

private object privateLock = new object();

void update(){
    lock(privateLock)
    {
        //change state 
    }
}

valueclass createclone(){
    lock(privateLock)
    {
        //Clone 
    }
}
Dave Bish
  • 19,263
  • 7
  • 46
  • 63
  • Thanks. Yes that seems to be the right option. By the way is there a specific reason why this approach is preferred over using [MethodImpl(MethodImplOptions.Synchronized)] ? – Piyush Mar 10 '14 at 15:14
  • 1
    Well - you actually need to use the same lock, over different methods - so, I'd suggest locking on an object. – Dave Bish Mar 10 '14 at 15:15
  • Does [MethodImpl(MethodImplOptions.Synchronized)] take the lock over this object? – Piyush Mar 10 '14 at 15:21
  • @piyush Synchronized flag tells compiler to make sure only one thread is in the function at a time. It won't prevent two treads being simultaneously on in clone second in update. And sound like you need to make sure that synchronization occurs across both functions. – LB2 Mar 10 '14 at 15:21
  • As I understand [MethodImpl(MethodImplOptions.Synchronized)] would take lock over this. If I add [MethodImpl(MethodImplOptions.Synchronized)] to both the methods, then access to them becomes synchronized right? – Piyush Mar 10 '14 at 15:23
  • @piyush: MethodImplOptions.Synchronized is normally not recommended as it takes the lock on the object itself (i.e. on the instance of your valueclass). This means that anybody outside the class can take a lock on the object so you no longer have control of your own lock object. – Jakob Christensen Mar 10 '14 at 15:26
1

You can also use the SyncRoot provided by ICollection interface

http://msdn.microsoft.com/en-us/library/bb348148(v=vs.110).aspx

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Oscar
  • 13,594
  • 8
  • 47
  • 75