A semaphore is a signaling mechanism used to synchronise two or more threads. Similar to a mutex, it can be used to protect shared resources. In this video, we show you how to use semaphores with FreeRTOS on an ESP32 to pass data between tasks.
The starting code for the challenge can be found here: https://github.com/ShawnHymel/introduction-to-rtos/blob/main/07-semaphore/esp32-freertos-07-challenge-counting-semaphore/esp32-freertos-07-challenge-counting-semaphore.ino
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-7-freertos-semaphore-example/51aa8660524c4daba38cba7c2f5baba7
Code for this video series (including demonstrations, challenges, and solutions) can be found here: https://github.com/ShawnHymel/introduction-to-rtos
A semaphore is similar to a mutex in that it is a locking mechanism used to tell tasks to wait if some resource is not available for use. However, semaphores are more than a simple lock: they can count to more than 1 and can therefore allow multiple threads to enter a critical section of code.
In practice, however, we often do not want to let multiple threads manipulate shared data, and we would still need to protect that data (or shared resource) with something like a mutex. So, we use semaphores as a signaling mechanism to tell other threads when it is safe to access a resource or read new data.
A buffer or linked list can be written to by a number of “producer” threads. Each time one of these producer threads adds data to this resource, it increments a semaphore, which is just a counting variable that can be accessed atomically. When a “consumer” thread wishes to read data from the resource, it decrements the semaphore count. If the semaphore is 0, that means no new data is available, so consumer threads must wait (e.g. enter the blocked state).
Semaphores work similarly to mutex, but are generally used in different circumstances. We provide examples of these use cases in the video as well as issue a challenge to use semaphores on your own.
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-7-freertos-semaphore-example/51aa8660524c4daba38cba7c2f5baba7
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