Save this video free

ECE344 Fall 2026 (Sec 1) Lec 16 - Locks

Jon Eyolfson · 43:54 · 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

Jon Eyolfson explains how data races arise when threads update shared memory and how mutexes protect critical sections. Using an eight-thread counter that should reach 8 million, he models `counter++` as a load, add, and store; explores possible interleavings and GCC GIMPLE; then shows why a plain-C check-then-set lock is unsafe and wastes CPU while spinning.

Key takeaways

Chapters

0:00 The Lost Increments Behind the 8-Million Counter
8:00 Data Races, Atomic Operations, and GCC GIMPLE
14:30 Enumerating Interleavings for Two Counter Increments
17:30 Using a POSIX Mutex to Protect a Critical Section
23:00 Choosing Critical-Section Size and Lock Placement
28:15 Lock Correctness: Safety, Progress, and Bounded Waiting
32:00 Why Disabling Interrupts Is Not a General User-Space Lock
33:00 The Plain-C Check-Then-Set Lock Fails

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

Transcript excerpt

6:43 really. >> Yeah. All right. >> Welcome back to operating systems. So, today we talk about locks. Not the fun kind of locks, the software locks. So recall what we had before we had eight threads all incrementing a million times should the end result should have been 8 million but when we ran it we got some like number that was just over 100,000 less than 200,000 that is because plus counter incrementing that global variable is actually three instructions on our CPU a load an add and a store. So what happened? Two threads both loaded for

7:30 example five the same value. Both would have done their increment uh locally to them because it would have been stored in a register. Then both would have written out six to memory aka an increment would have got lost or it would have used a stale value. So today we get to talk about exactly what goes wrong and how to fix it with a lock. So most probably the like one of the most important definitions in this course, you should know this one off by heart. So we'll say it a data race is when you have two accesses to the same memory location and at least one is a right. So they can be two concurrent accesses. So also accesses in parallel to the same variable or memory location.

8:18 And again at least one of them is a right. So in this example, we had two threads reading or sorry, if we had just two threads reading counter, that's fine. They're just going to both read the same value. They're not going to get an old still value. As long as no one's writing to it, absolutely fine. But as soon as a thread starts to write to it,

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.