Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a lock

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.

like image 565
newprogrammer Avatar asked Jan 16 '23 23:01

newprogrammer


1 Answers

With the old, standard C++, you cannot implement your own lock, since the lock variable itself is in a data race.

C++11 and C11 add atomic variables, which you can use for precisely this purpose; e.g. in C++:

#include <atomic>

std::atomic<bool> isLocked;

bool lock() { return !isLocked.exchange(true); }

void unlock() { isLocked = false; }

The key here are the atomic exchange and the (implicit) atomic store, which generate special hardware instructions and are always race-free, and which you cannot "fake" with ordinary variables.

like image 68
Kerrek SB Avatar answered Jan 25 '23 04:01

Kerrek SB