I have method Test
, that can be executed from many threads, and I don't want to lock all threads, who want execute them, while method already executing. My idea is create volatile isProcessing
and set it to true
, while execution is active.
But I don't know the C# memory model, and can't understand when other threads will see new value of isProcessing
. Will it do instantly after setting isProcessing
value or only after quit from lock
section?
Code sample:
private volatile bool isProcessing = false;
private void Test()
{
if (isProcessing) return;
lock(this){
try{
isProcessing = true;
//do something
}
finally{
isProcessing = false;
}
}
}