This episode dives deep into locks. How do they work? And then explains how you can use this to implement a reader-writer lock.
A basic lock can be created with the “set and test” instruction. This works by reading a current memory value, setting it to one, and returning the original value. You can build a simple lock by repeatedly calling this method until it returns something other than one. You probably don’t want to go into a busy loop like this just calling the same method over and over again. The idea is to call this method though until it returns something other than one.
Then with two such locks and an integer count variable, this episode explains how you can build a reader-writer lock.