I have two functions in different classes, and they are generating race condition.
One function is updating variables value and another one is using variable, and they are in Race condition my assumption.
countervariable is accessible from both functions.var counter=0;Both functions are in different classes.
Function updating value
protected void updateValue(SomeClassType val){
// Based upon some condition value for variable is updated
if(val.id){ // some condition
counter=2;
}
}
Function using value
protected void useValue(var counter){
// Do something with variable.
}
- The
upateValuefunction is running inworker-thread. anduseValueis running inmain-thread. - What I need is
useValueshould wait tillupdateValue, updates variable value.
What I Tried
- Created one static object of
AutoResetEvent.
public static AutoResetEvent autoResetEventForUpdateValue = new AutoResetEvent(false);
protected void updateValue(SomeClassType val){
// Based upon some condition value for variable is updated
if(val.id){ // some condition
counter=2;
}
autoResetEventForUpdateValue.Set();
}
Function using value
WaitHandle[] waitHandles = new WaitHandle[] { autoResetEventForUpdateValue};
protected void useValue(var counter){
// Do something with variable.
}
Also
updateValue(val), this parametervalof typeSomeClassTypeis not accessible in implementation of classuseValue(counter).
- With my solution while debugging I realized that
useValuekeep waiting andupdateValueis not initialized asworker-threadnot running there.
I would just change
UpdateValueto return the value instead of updating it in place, that should make ordering trivial:Pure methods, i.e. methods without side effects, where the result only depend on the input, tend to be the easiest to use. This is especially true when dealing with multi threading.
You might also want some mechanism to ensure your method cannot run concurrently. If this is triggered by a button press you could disable the button before you call
ComputeNewCounterValueand enable it afterUseValue;