Save this video free

ECE344 Fall 2026 (Sec 1) Lec 18 - Semaphores

Jon Eyolfson · 57:00 · Watch on YouTube

Part of University of Toronto ECE 344. See all 28 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 distinguishes mutexes, which protect shared data from races, from semaphores, which coordinate the order in which threads proceed. He develops the semaphore wait/post model through ordered-print examples and applies it to a bounded producer-consumer buffer, using one semaphore for filled slots and another for empty slots while retaining mutexes to protect shared indices.

Key takeaways

Chapters

0:00 Opening: Locks, Data Races, and Thread Ordering
8:30 Ordered Print: Thread Creation Does Not Guarantee Output Order
11:00 Why a Shared Flag and Busy-Wait Loop Are Poor Coordination
14:30 Why Reusing a Mutex for Signaling Is Undefined Behavior
20:00 MT-Safe Functions and the Cost of Serializing Threads
22:45 Semaphore Basics: Atomic Wait, Post, and Initialization
26:30 Enforcing Ordered Print with a Zero-Initialized Semaphore
32:30 Chaining Semaphore Signals and Comparing Semaphores with Mutexes
37:00 Producer-Consumer Buffer: Identify Empty Reads and Overwrites
44:30 Two Semaphores Prevent Empty Reads and Full-Buffer Overwrites
53:00 Protect Shared Buffer Indices and Avoid Semaphore Deadlock

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

Transcript excerpt

1:59 Mhm. >> Mhm.

7:44 >> Mhm. >> Alrighty, welcome back to operating

8:33 system. So, fun day of semaphores. Yay! So, as a recap, so locks gave us mutual exclusion. Have we all remembered what data races are from the top of our head? Two concurrent accesses to the same memory location variable with at least one being a right. If you want to prevent data races, you can use a mutex because anything between the lock and unlock call is a critical section we call where only one thread can be executing that code at a time. Therefore, if only one thread can be executing that code at a time, don't have concurrent accesses, don't have data races. Joy. So, we even built one in lecture 17 with compare and swap or the stupid comp exchange func- or

9:23 assembly instruction. Same thing. And then we solved the bounded wait or the busy wait problem by sticking on a wait queue for them. So, a lock, however, it say which thread gets there first. Like how would we make one thread always run before another? So, this is mostly fun coding examples today. So, let's take this code. So, main thread, I create two threads. I create them. One where is destined to run this print second function. The other is destined to run print first. So, I create them. Both of them. Then, the main thread joins them so that they're all cleaned up.

10:10 And everything and terminated before the main thread return zero, which is the same as exit, which exits the process. And then, print first prints off this is first. Print second prints off I'm going second. As written, if I run this program, what's going to happen? Yeah. >> One of them will print before the other.

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.