Introduction to RTOS Part 6 - Mutex | Digi-Key Electronics

Published at : 23 Dec 2025

A mutex, short for MUTual EXclusion, is a locking mechanism that is used to prevent other threads from interrupting or overwriting a section of shared data (such as a global variable or common buffer).

The solution to the challenge in the video can be found here: https://www.digikey.com/en/maker/projects/introduction-to-rtos-solution-to-part-6-freertos-mutex-example/c6e3581aa2204f1380e83a9b4c3807a6

Code for this video series (including demonstrations, challenges, and solutions) can be found here: https://github.com/ShawnHymel/introduction-to-rtos

FreeRTOS implements mutexes and semaphores with queues, and operations to read, increment, and decrement these kernel objects are all atomic (meaning other threads cannot interrupt or modify the variables during those operations).

We use a mutex as a locking mechanism to protect a shared resource or critical section of code. When a thread or task takes a mutex, the value of the mutex is decremented from 1 to 0, and other tasks may not take the mutex while it is 0.

When the task is done working in the critical section, it gives the mutex back, which increments it from 0 to 1. This action of taking and giving a mutex allows only one thread to operate in a critical section of code at a time (it is mutually exclusive: no other threads may execute that section or take the mutex during that time).

In the video, we give an example of a race condition and how a mutex can be used to prevent it from happening. We provide a demonstration of using a mutex in FreeRTOS and then issue a challenge to use a mutex to allow parameters to be passed to tasks (note: this is a hack!).

Product Links:
https://www.digikey.com/en/products/detail/adafruit-industries-llc/3405/7244967

Related Videos:
Introduction to RTOS Part 1 - What is a Real-Time Operating System (RTOS)? - https://youtu.be/F321087yYy4​

Introduction to RTOS Part 2 - Getting Started with FreeRTOS - https://youtu.be/JIr7Xm_riRs​

Introduction to RTOS Part 3 - Task Scheduling - https://youtu.be/95yUbClyf3E​

Introduction to RTOS Part 4 - Memory Management - https://youtu.be/Qske3yZRW5I​

Introduction to RTOS Part 5 - Queue - https://youtu.be/pHJ3lxOoWeI​

Introduction to RTOS Part 6 - Mutex - https://youtu.be/I55auRpbiTs​

Introduction to RTOS Part 7 - https://youtu.be/5JcMtbA9QEE​

Introduction to RTOS Part 8 - https://youtu.be/b1f1Iex0Tso

Introduction to RTOS Part 9 - https://youtu.be/qsflCf6ahXU

Introduction to RTOS Part 10 - https://youtu.be/hRsWi4HIENc

Introduction to RTOS Part 11 - https://youtu.be/C2xKhxROmhA

Introduction to RTOS Part 12 - https://youtu.be/LPSHUcH5aQc

Related Project Links:
https://www.digikey.com/en/maker/projects/introduction-to-rtos-solution-to-part-6-freertos-mutex-example/c6e3581aa2204f1380e83a9b4c3807a6

Related Articles:
https://www.digikey.com/en/maker/videos/shawn-hymel/getting-started-with-stm32-and-nucleo-part-3-how-to-run-multiple-threads-with-cmsis-rtos-interface

Learn more:
Maker.io - https://www.digikey.com/en/maker

Digi-Key’s Blog – TheCircuit https://www.digikey.com/en/blog

Connect with Digi-Key on Facebook https://www.facebook.com/digikey.electronics/

And follow us on Twitter https://twitter.com/digikey