I’m trying to understand how locks work.
Let’s say I want to implement a really simple lock in C++
class Resource{
public:
bool lock();
void unlock();
... methods to change/read the Resource ...
private:
bool isLocked;
}
The user of the Resource calls lock(), and if isLocked is true, then lock() returns false, and the user of the Resource has to either wait, or do something else. If isLocked is false, then lock() sets isLocked to true, and returns true. Then the caller can do whatever he wants to the resource. He calls unlock() on the resource afterwards to set isLocked to false.
However, what if two users of the resource call lock() at precisely the same time? Does this situation rarely happen? I think more formally, this involves making the lock() operation “atomic”, though I’m not precisely sure what that word means.
“Atomic” means that the operation can’t be interrupted. That is, you can be sure that the semantics of that operation are the same regardless of the behaviour of other threads/processes. You’re right that something in your
lock()call will likely have to be atomic. Most architectures provide some helpful instructions with guaranteed atomic behaviour – you might also find some libraries built on those operations to give you more flexibility at the higher layer you’re programming at.