To complement, the lock
It’s a kind of traffic light. In fact through it it is possible to deploy a data structure capable of controlling the processing traffic that is called traffic light.
The lock
turns on a red light telling any code that tries to access that chunk of memory it’s forbidden to do until the light goes out. Note that although it looks like a traffic light, it is not in fact. That’s why lock
in itself can be used at a traffic light which is a somewhat more sophisticated structure.
It indicates for the entire application that at that time something will be done with the object that cannot be stopped. That no part of the application can consider the state of the object until it is released because the operation at that time may be inconsistent or incomplete.
When your code is compiled statement lock
becomes a code equivalent to this:
bool lockWasTaken = false;
var temp = thisLock;
try {
Monitor.Enter(temp, ref lockWasTaken);
if (amount > balance) {
throw new Exception("Insufficient funds");
}
balance -= amount;
} finally {
if (lockWasTaken) {
Monitor.Exit(temp);
}
}
I put in the Github for future reference.
Note that it is possible to get the same result using only the library but not recommended.
In codes you’re sure you’ll never have threads you do not need and should not use this type of synchronism. For some operations the performance cost of executing class methods Monitor
may be prohibitive.
See the Reference Source of . NET to understand the inner workings of the class (not that it helps much :D).
+1, excellent mention to the semaphore structure.
– OnoSendai