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

Опубликовано: 18 Октябрь 2024
на канале: DigiKey
98,742
1.4k

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/proj...

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

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/d...

Related Videos:
Introduction to RTOS Part 1 - What is a Real-Time Operating System (RTOS)? -    • Introduction to RTOS Part 1 - What is...  ​

Introduction to RTOS Part 2 - Getting Started with FreeRTOS -    • Introduction to RTOS Part 2 - Getting...  ​

Introduction to RTOS Part 3 - Task Scheduling -    • Introduction to RTOS Part 3 - Task Sc...  ​

Introduction to RTOS Part 4 - Memory Management -    • Introduction to RTOS Part 4 - Memory ...  ​

Introduction to RTOS Part 5 - Queue -    • Introduction to RTOS Part 5 - Queue |...  ​

Introduction to RTOS Part 6 - Mutex -    • Introduction to RTOS Part 6 - Mutex |...  ​

Introduction to RTOS Part 7 -    • Introduction to RTOS Part 7 - Semapho...  ​

Introduction to RTOS Part 8 -    • Introduction to RTOS Part 8 - Softwar...  

Introduction to RTOS Part 9 -    • Introduction to RTOS Part 9 - Hardwar...  

Introduction to RTOS Part 10 -    • Introduction to RTOS Part 10 - Deadlo...  

Introduction to RTOS Part 11 -    • Introduction to RTOS Part 11 - Priori...  

Introduction to RTOS Part 12 -    • Introduction to RTOS Part 12 - Multic...  

Related Project Links:
https://www.digikey.com/en/maker/proj...

Related Articles:
https://www.digikey.com/en/maker/vide...

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   / digikey.electronics  

And follow us on Twitter   / digikey