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/c6e3581aa2204f1380e83a9b4c3807a6Code 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/7244967Related Videos:
Introduction to RTOS Part 1 - What is a Real-Time Operating System (RTOS)? -
https://youtu.be/F321087yYy4Introduction to RTOS Part 2 - Getting Started with FreeRTOS -
https://youtu.be/JIr7Xm_riRsIntroduction to RTOS Part 3 - Task Scheduling -
https://youtu.be/95yUbClyf3EIntroduction to RTOS Part 4 - Memory Management -
https://youtu.be/Qske3yZRW5IIntroduction to RTOS Part 5 - Queue -
https://youtu.be/pHJ3lxOoWeIIntroduction to RTOS Part 6 - Mutex -
https://youtu.be/I55auRpbiTsIntroduction to RTOS Part 7 -
https://youtu.be/5JcMtbA9QEEIntroduction to RTOS Part 8 -
https://youtu.be/b1f1Iex0TsoIntroduction to RTOS Part 9 -
https://youtu.be/qsflCf6ahXUIntroduction to RTOS Part 10 -
https://youtu.be/hRsWi4HIENcIntroduction to RTOS Part 11 -
https://youtu.be/C2xKhxROmhAIntroduction to RTOS Part 12 -
https://youtu.be/LPSHUcH5aQcRelated Project Links:
https://www.digikey.com/en/maker/projects/introduction-to-rtos-solution-to-part-6-freertos-mutex-example/c6e3581aa2204f1380e83a9b4c3807a6Related 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-interfaceLearn more:
Maker.io -
https://www.digikey.com/en/makerDigi-Key’s Blog – TheCircuit
https://www.digikey.com/en/blogConnect with Digi-Key on Facebook
https://www.facebook.com/digikey.electronics/And follow us on Twitter
https://twitter.com/digikey