Mutex Behavior with Multiple Tasks

This is my first post, and I am seeking some clarification regarding the usage of mutexes in FreeRTOS.

According to the definition, mutexes ensure that only one thread can access a shared resource at a time. In a scenario with two tasks and one shared variable, if a mutex allows only one task to access the resource, how does the other task gain access to and protect the shared resources?

First, Mutex by themselves don’t enforce the requirement that only one task at a time has access, that only happens if you use the Mutex per its requirements.

The Mutex only lets itself be given to one task at a time, so only if tasks first take the mutex, then access, and then give back the mutex, do you get protection.

Note, this means the must be just ONE mutex that is shared by the tasks wanting protected access, then each task, when it want to access the resource, takes the mutex (and gets blocked for a while if the resource is busy), then, when it has gotten the resource, it can use it knowing that as long as every other task is doing the same, it has exclusive access, and then when it is done, it needs to give back the mutex, so some other task can get it.

1 Like

@Gogo00 I would recommend reading through the free book here: Free RTOS Book and Reference Manual It’s out of date, but a quick read, and explains FreeRTOS mutexes.

1 Like