Save this video free

ECE344 Fall 2026 (Sec 1) Lec 17 - Building Locks

Jon Eyolfson · 1:05:39 · Watch on YouTube

Part of University of Toronto ECE 344. See all 26 lectures and search them

These notes were made with YouTube Collector. Paste any YouTube link and get the same: a summary, chapters and a searchable transcript.

Overview

The lecture builds from an unsafe check-then-set lock to atomic compare-and-swap spin locks, then shows why efficient sleeping mutexes need coordinated queueing to prevent lost wakeups and unfair lock acquisition. It explains how Linux futexes provide a fast user-space path with kernel-assisted waiting, and how read-write locks allow concurrent readers while introducing writer-starvation risks.

Key takeaways

Chapters

0:00 From a Broken Check-Then-Set Lock to Atomic Operations
11:00 Compare-and-Swap Makes Lock Acquisition Atomic
16:50 Hardware Instructions and the Basic Spin Lock
21:10 When Spinning Helps, and xv6's Spin-Lock Tradeoff
24:10 A Queue-and-Sleep Design for Contended Locks
28:10 Naive Queueing Causes Lock Stealing and Lost Wakeups
36:30 Use an Internal Spin-Lock Guard to Protect the Protocol
40:00 Transfer Mutex Ownership Directly to a Queued Thread
44:10 Handle the Race Between Releasing the Guard and Sleeping
52:00 Linux Futexes Combine User-Space Fast Paths with Kernel Waiting
56:00 Futex Mutex States and the Cost of Contention
58:18 Read-Write Locks Allow Concurrent Readers
1:01:00 Build a Read-Write Lock and Identify Writer Starvation

Keep these chapters and the full searchable transcript in your own library.

Transcript excerpt

8:12 All righty. Let's resume a bit early so we can get out here. It's a Friday. So, building locks. We made a real real real real bad lock. So, we're going to do it better. So, this was our lock. Wasn't safe. We can argue about two threads coming in with initial value of one. Both just do the memory read. See that the value is zero. Both set to one. Boom. Both pass. Making our lock completely useless for mutual exclusion. So checking the lock and taking it has to be one atomic step in order to actually implement this easily. Disabling interrupts again that would make it true in one CPU, not on several. So, we're not going to get an easy out on this. So,

9:00 this came up. You can actually implement locks in software with minimal hardware support. So if your hardware just has loads and stores being atomic and instructions execute in order, which is not true for any modern CPU, but was true back in the day, you can implement locks that are safe using something called Peterson's algorithm or Lampard's bakery algorithm. So, if anyone's ever been to an old school bakery where you like pick a number and then they're called out in order and that basically that's the idea behind it. It's going to be hella slow and also instructions executing in order is not true. So, we'll see what's actually used. So, both

9:49 of these don't scale well and again complete lie. Modern processors execute out of order. So that is both the CPU reordering your instructions as you've written them and your compiler reordering loads and stores because they look independent to the compiler and the compiler can do whatever it wants to as well. So let's assume we have this

This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.

In this course

Related notes

Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Jon Eyolfson.

Want the full transcript?

Save this video in YouTube Collector to get its complete searchable transcript, your own AI summaries, and a library that keeps every video you collect in one place.