100: Multithreading. Master The Lock.
Take Up Code - En podcast af Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Kategorier:
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. Listen to the full episode or you can also read the full transcript below. Transcript So far, when describing multithreading, I’ve mentioned locks and also that there’s some builtin ability that makes all this possible. What is that exactly? I don’t want you thinking that there’s some magic involved. We’re going to dive deep and explore how locks really work and then what you can do with them. Imagine for a moment an ancient meeting between several local tribe leaders. They kept interrupting each other and couldn’t get anything done. One day, a powerful leader had enough and brought his wooden club to the meeting. Not to hit other leaders. That would just start a war. But to bang the club loudly on the stone floor before speaking. This got the attention of the other leaders long enough for him to speak. In the next meeting other leaders tried to bring their clubs too but the one leader was smart enough to recognize the threat and made a deal that only one club would be allowed in the meeting. Anybody who wanted to speak would be allowed to bang the club. Eventually, the whole banging the wooden club on the floor thing was replaced with just the need to hold some symbol and whoever currently held the symbol would be allowed to speak. This caveman type story can actually be used by modern processors. Some processors might have developed more sophisticated methods. I haven’t done a lot of research into all the ways locks can be implemented so maybe there are other ways. I’ll just explain this method so at least you understand how it all could work. This system relies on possession of a physical object. That’s a problem for threads running on processor cores. What can a thread possibly hold onto? There’s really only one thing and that’s a specific value stored in some memory location. All it requires is the ability to execute a single instruction that can’t be interrupted by any other thread or processor. In the case of multiple processors, the system will likely need to make use of a single memory manager common to the whole computer. This single instruction needs to be very simple. It’s called a set and test instruction. The name’s a bit misleading. To me, it should have been called the set and return original value instruction. But you can probably see why set and test is a shorter and more catchy name. Here’s what it does though. The instruction goes to a specific memory address, reads the value currently stored there, and then sets the value to one. It then returns the original value. That’s it. Just go get the current value and make the value one. The question then is what good is this? Well, it turns out that this simple instruction treats anything other than a one as a special value. Let’s start out with the value set to zero. That’ll be the initial state when no thread has a lock. Now, let’s take two threads as an example and these two threads both want this lock. It’s a race and they each execute the set and test instruction at the same time. Only one of them will be allowed access to the memory location though. The other will get a busy response and will have to wait. This is sort of like the