# Can we "give" Mutex in ISR

**URL:** <https://forums.freertos.org/t/can-we-give-mutex-in-isr/9120>\
**Category:** Kernel\
**Created:** [March 2, 2020, 8:54pm UTC](https://forums.freertos.org/t/can-we-give-mutex-in-isr/9120 "2020-03-02T20:54:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhmkmhyh](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/nhmkmhyh/32/163_2.png) [@nhmkmhyh](https://forums.freertos.org/u/nhmkmhyh)\
**Post date:** [March 2, 2020, 8:54pm UTC](https://forums.freertos.org/t/can-we-give-mutex-in-isr/9120/1 "2020-03-02T20:54:37Z")

</div>

It is mentioned in multiple freeRTOS documentations that Mutexes should not be used from an interrupt (ex: [https://www.freertos.org/Real-time-embedded-RTOS-mutexes.html](https://www.freertos.org/Real-time-embedded-RTOS-mutexes.html)). I do understand why we shouldn’t “take” a mutex in an interrupt (blocking call/priority inheritance logic) but I am not clear why we shouldn’t be able to “give” a mutex from an interrupt?

Today if I can’t “give” a mutex in an ISR, I’d have to create what seem to be a redundant synchronization semaphore btw the calling task and the ISR and implement mutual exclusion within the task.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [March 2, 2020, 9:18pm UTC](https://forums.freertos.org/t/can-we-give-mutex-in-isr/9120/2 "2020-03-02T21:18:54Z")

</div>

The key point on this is that the mutexes have special code optimized for use in resource management to provide exclusive access (mutex = mutual exclusion).

As such, the usage pattern for a mutex should always be:

Take the mutex (then if successful)  
Use the resource  
Release the mutex.

As such, the mutex should always be given back by the task that took it. If anyone else releases it, then the possible sequence could be

Process A takes the Mutex  
Process A uses the Resource  
Someone else releases the mutex  
Process B takes the Mutex  
Process A and B both try to use the resource !!! Bad things might happen.

If there really is a need for someone else to be able to give the flag back, then you should be using a semaphore (and carefully think if you are creating a problem).

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [March 2, 2020, 9:27pm UTC](https://forums.freertos.org/t/can-we-give-mutex-in-isr/9120/3 "2020-03-02T21:27:29Z")

</div>

Additionally mutexes are associated with tasks because they include a (task) priority inheritance mechanism, which is out of context with in interrupt.

---

<div class="post-metadata">

**Author:** ![9a3eedi](https://avatars.discourse-cdn.com/v4/letter/9/f9ae1b/32.png) [@9a3eedi](https://forums.freertos.org/u/9a3eedi)\
**Post date:** [March 3, 2020, 4:22am UTC](https://forums.freertos.org/t/can-we-give-mutex-in-isr/9120/4 "2020-03-03T04:22:27Z")

</div>

It sounds to be that what you really should be using is a Binary Semaphore, not a mutex. While they both use SemaphoreHandle\_t in FreeRTOS, they have different use cases. A binary semaphore is simply a semaphore with a count of one. A mutex is used specifically for mutual exclusion. It is a perfectly normal usecase for an interrupt to give a semaphore to a binary semaphore. It’s not normal for an interrupt to unlock a mutex.

Even the termininology between binary semaphores and mutexes are different. Mutexes are generally said to be “locked” and “unlocked”, not “given” and “taken” as in a semaphore. So you should be thinking about them in a different way.
