← All transcripts

Atomic Operations - Computerphile Transcript, AI Summary & Key Points

Computerphile · 12 days ago · Education · 09:46 · EN-GB

Watch on YouTube

AI Summary

Matt Godbolt builds on the previous spin-lock discussion to show why spin locks are unfair and how a small extra hardware primitive — atomically adding to a memory cell — enables a fair ticket-based lock. A deli counter ticket dispenser serves as the real-world analogy: taking a ticket atomically increments the ticket number, the holder waits until the 'now serving' counter reaches their number, and the unlock step bumps the 'now serving' value. This guarantees a place in queue so no thread starves. He covers failure modes — a ticket holder walking away stalls the queue, and timeouts get complicated — plus policy refinements like multiple queues or express lanes, and priority inversions. He also describes double-checked locking for once-only initialisation, such as a shared database connection, where a fast unlocked check skips locking overhead once connected. Finally, he notes that spinning is expensive: the CPU pegs at 100% doing nothing useful.

Key Points

  • Background — the earlier spin lock works by atomically putting a new number into a memory cell and getting the old value out, but it is unfair: with many threads contending, one can lose every time, like someone repeatedly beaten to the door handle by a faster counter
  • The fairness problem can be fixed with one new hardware facility: an atomic operation that adds to (or subtracts from) a memory cell rather than just replacing it — some implementations return the new value, some the old, and you account for the difference either way
  • Deli analogy — a customer takes a ticket (atomic increment of the 'next ticket' counter), waits watching the 'now serving' LED, and on leaving increments 'now serving' so the next person knows it is safe to enter; that bump is the unlock
  • A ticket lock is fair because a ticket fixes your place in the queue, so you cannot be starved
  • Failure mode — if a ticket holder wanders off and misses their number being called, everyone behind is stuck; in computing terms adding a timeout reintroduces contention over who may skip the queue, and the skipped thread returning makes it very complicated
  • Policy layer — different-length or multiple queues handle mixed short and long operations, like the basket-only till, but this quickly raises priority inversion: a low-priority long task holding the lock while a higher-priority one waits
  • Performance optimisation — for once-only initialisation like a database connection, check whether the work is already done before taking the lock; if connected, skip locking entirely, though a writer must double-check after acquiring the lock because someone else may have connected in between
  • Spinning is expensive — the busy-wait pegs the CPU at 100% doing no useful work while waiting for the lock

Tools & resources

1 item

Links mentioned

🔒 Full analysis locked

Unlock more videos and the full analysis

A credit unlocks one video's full analysis for good — the build steps, the tools and how each was used, the methods behind every use case. Pro opens the whole library instead, and raises how many videos you can analyse a day.

Unlock full analysis — free

Transcript

Searchable transcript of Atomic Operations - Computerphile — Computerphile (09:46). Search for a phrase, then click its timestamp to jump straight to that moment in the video.

Captions sourced from the original video on YouTube, published by Computerphile. The video, its captions and all related intellectual property remain the property of their respective owners; AINotes claims no ownership. Provided for research, accessibility and search — see the Transcript Notice and Copyright Policy.

00:00 There's something I'd like to talk about because one thing about mutxes and mutx is kind of a general term. The particular type of mutual exclusion that we went into last time was called a spin lock because we just spam trying to get the lock or not. The thing about that is it's not very fair if you've got 20 people that are all trying to get into the bathroom and they're all basically trying the door handle at the same time.

00:21 Now, obviously that doesn't work as well, but like maybe if you're a little bit slower at trying the door handle or if you know as you're going try the door handle, okay, count to 10, try the door handle, count to 10, like you might do, you might just be really unlucky and the guy who's counting to nine gets in front of you every single time and then you never actually get in the bathroom and then it's not worth thinking about, right?

00:41 So if we want to be fair about this, there's other ways of doing this, but it does require a new facility that the hardware can give us. And so like think of what's a what's a situation in real life where you are in uh you're waiting for exclusive access to something. I so the I many many years ago I used to work on the delicatessan at a local supermarket and the solution we had for remembering who was next was to take one of those little tickets off of the um off of the counter and now I've got a number and on on the

01:19 other side me as the server I know which number customer I'm currently serving and so we can actually build this into a fairer log and the way that we're going to do that is we need one new thing we need the ticket dispenser. And so the hardware, if you remember, we went back um and we said that like with one operation, the ability to put a number into a box and replace it with another number.

01:42 There's something else we could do, right? That's a relatively straightforward thing. So like just to recap, we've got our uh memory of cell over here and it's got the number zero. And our previous spin lock relied on us being able to put a new number in, say the number one into that box and atomically, that is with no one else getting in there and monkeying with it, get the old value out.

02:05 So we would get the zero out and now there would be a one in the box. That's useful. We can build things with it. But what if we just gave it a tiny tiny bit more um ability? So let's say instead of me replacing the number, I'm going to do a very simple piece of arithmetic on it. like I can add to it or subtract from it. Let's just go with those for now.

02:25 So now what I can do is I can say I'd like to plus one whatever is in the box now. And so if there's zero in the box that was from before what we're going to do is we're going to get the zero out of the box and we'll have added one to it. Whatever was in there before. And so atomically we're able to increment a memory location and we know that we added one.

02:52 Now so some implementations actually return the value of having added the one to the thing and give you back a one out here. Other ones say no I give you the old value. But either way you know the number you just added to it. So you just add it to it if you to account for it. So you now know whichever. So in this instance we've now got our ticketing system.

03:08 This becomes our ticket number. You are number this in the queue. And so uh a simpler or a simple ticketbased lock is the um the server if you like the person who is saying like you this is now now serving number three or whatever has their number over here. So now serving now serving and let's say it starts at um right we're going to start at zero over here and we're going over here we're going to say the next ticket over here is we're also going to put zero in it and then when we switch in it we're going to add our

03:47 one to it. So that means that when we when we go we're going to say I'd like to get a ticket please plus one into there and we get now the next one becomes a one and atomically we also know that we got it was zero before so we are now ticket number one and now we can just sit here waiting for the now serving to finish whatever they were doing before and then they move on to one and say okay now I'm serving one so we can now spin here waiting saying like keep reading the now serving thing Keep looking up at the now

04:17 serving until it says one. In this particular case, like you're serving yourself from the delicates, right? I know there isn't because otherwise we have to have two different people involved. But like imagine that you took your ticket, the now serving says your number, you go and get serve yourself from the deli and then on your way out, you add one to the now serving so that the next person knows it's safe for them to go in.

04:35 So that's sort of like the unlock process is that they they press the button behind the back of the deli that, you know, bumps the number of the big LED sign at the top. And so now what we've got is a fair way because if you um get a ticket, you've got a place in Q and or in line and now you're not going to be stuck waiting forever. So that's what is has been unlocked just by us having this atomic operation where we can add one to a cell or add any number in fact.

05:01 And so it's fair because you're in you've got your place in line. It's set. There's not any one uh methodology. Um, obviously the problem a problem with this is if you uh aren't paying attention to the dead counter number. If you took number three and then you went and went, you know, shopping somewhere else in the supermarket and then number three is called, everyone is now stuck.

05:26 >> And that probably happens in real life as much as they think. >> Right. Exactly. You know, and if you're the Delhi person, you uh you shout out number three, number three. And eventually you give up on number three and then they come back and get really angry that you've missed them. But in computing terms like if you wanted that kind of process to happen if you wanted to have like some kind of timeout or whatever now everybody is arguing about whether or not they are okay maybe number four gets the ability to like

05:49 say okay once we've waited long enough I will just go in and take it but you know then number three could come back and it it gets very very complicated >> it's it's quite simplistic as well isn't it so imagine I don't know say there were two or three operations and one of them is going to take a really long time and actually the realistic thing will be to do the two or three that are short very quick ones and then do the long one afterwards, isn't it?

06:13 >> Possibly. Now we're into sort of policies that you can build using these primitives that we've got. Perhaps you would have different length cues. Maybe you have multiple cues. Um uh not cues, you know, counters. Um and you know, if you know that you're doing a short piece of work, you go and take a ticket from the I'm just going to be quick, I promise part, >> the basket only till in the uh in the supermarket.

06:32 >> Yeah, great analogy. Yeah, exactly. But, you know, very quickly you get into things like priority inversions, which is this this idea that yeah, somebody who's got a relatively low priority but maybe long running task could have the the lock and somebody who's more important than them is just waiting for it and there's not an easy way to get around that.

06:47 But these are this is where it gets into the sort of like subtlest of what did you mean to happen? What would you like to happen at this kind as opposed to like the primitives that mean it's safe to do the work going forward like you know like maybe you're deadlock but you'll never do anything wrong in this instance. Yeah. Yeah, I never thought I'd be bringing my teenage job up uh professionally.

07:08 >> Well, I worked in a similar kind of job, but it was a much smaller shop, so we didn't have any tickets or anything. You had to try and keep track of who was next >> of who was there. Yeah. >> Cutting ham and slicing cheese. >> Pragmatically, the machine was out of order more often than it was in order. So, we would just do the same thing, you know, and obviously there's a sort of point where you look out and go like, there's only one person.

07:25 we don't have to go through this rig rigma roll here which interestingly is is actually uh something that we do do in higher performance locking code. Um sometimes you can do a thing where you check first and then if it's clearly okay you you can kind of miss the whole locking part itself. So for example often in code you want to do something exactly once right you like the first person to like call this function or do this operation has to connect to the database or has to and that's maybe a long lived operation then

07:59 you keep the connection up after that forever more but obviously lots of people could be trying lots of threads could be trying to connect to run the first operation at the same time. So you pro one solution to that is to have a lock of some sort be it a spin lock or any probably a spin lock in this instance that says like okay or a mutx am I okay to connect to the database or I or am I already connected and if I'm already connected then okay drop the lock we're done nothing to do here and that's fine but if we're not

08:27 connected then hold the lock and everyone's backing up behind me while I do this long lived connection to the database now we've got the database now we can all share the database connection something like that the um the downside to that is that like once you've connected a database like once at the beginning of your program you're doing all this locking for nothing right so you what you might do instead is this sort of optimistic if the database is already there and you can do some kind of safe thing then I can use

08:50 it without doing all of the locks and there are some subtleties around that because as somebody who is is about to write to the database you know you have to do a sort of double check where I go like well maybe if I if I if the database isn't there it's not safe for me to go and connect to the database cuz now I do have to get the lock and then I have to check again because while I was doing that someone else may have actually got off and connected to the database and so you're in this sort of blurry world between the

09:16 two but for performance reasons sometimes you might want to do that um that quick check first continuously going how have you are you done yet? Are you done yet? Are you done yet? Obviously, that's that's very irritating for the person on the other side of the door. And it's not free in computer terms as well. This is an expensive operation. You're pegging the CPU 100%. It's doing 100% work. It's not waiting for anything. It's just literally spinning and spinning and spinning and spinning.