internal static void ExecuteCommand(NLO nlo)
{
switch (nlo.action)
{
//synchronize each method in case
case (NLO.Actions.SET):
swSet(nlo);
break;
case (NLO.Actions.ADD):
swAdd(nlo);
break;
default:
throw new System.Exception("Error");
//break;
}
return;
}
i want to lock so each of the methods swSet and swAdd are able to run in paralell, but lock swSet from running in concurrent with itself and same for swAdd
Is the easiest way to put locks around each case or to have the lock in the methods themselves or to have [MethodImpl(MethodImplOptions.Synchronized)] on the method?
Will the last solution lock all methods?
[MethodImpl(MethodImplOptions.Synchronized)]is not overly documented, but will generally use the same lock object for all such methods in the same type; from MSDN:Frankly I never find that attribute useful. In your case, it sounds like you need separate locking for the two methods: