I have multiple threads accessing a single Int32 variable with "++" or "--" operation.
Do we need to lock before accessing it as below?
lock (idleAgentsLock)
{
idleAgents--; //we should place it here.
}
Or what consequences will there be if I don't do the locking?
It is not "atomic", not in the multi-threaded sense. However your lock protects the operation, theoretically at least. When in doubt you can of course use Interlocked.Decrement
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With