.net - resolving threading conflicts in C# -
lets suppose there static variable accessed 2 threads.
public static int val = 1;
now suppose thread 1 execute's statement this
if(val==1) { val +=1 }
however after check , before addition in above statement thread 2 changes value of val else.
now cause nasty error. , happening in code.
is there way thread 1 gets noticed values of val has been changed , instead of adding goes , performs check again.
specifically example, could:
var originalvalue = interlocked.compareexchange(ref val, 2, //update val value 1); //if val matches value if(originalvalue == 1) { //the update occurred }
Comments
Post a Comment