# Best practice to share same resources between tasks

**URL:** <https://forums.freertos.org/t/best-practice-to-share-same-resources-between-tasks/1690>\
**Category:** Kernel\
**Created:** [March 12, 2015, 9:13am UTC](https://forums.freertos.org/t/best-practice-to-share-same-resources-between-tasks/1690 "2015-03-12T09:13:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 12, 2015, 9:13am UTC](https://forums.freertos.org/t/best-practice-to-share-same-resources-between-tasks/1690/1 "2015-03-12T09:13:27Z")

</div>

**[chaabanemalki](https://sourceforge.net/u/chaabanemalki/)** wrote on [Thursday, March 12, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/4d47b5cb/#8490):

Hello,

I’m working on a project where I have to write to an SD card from more than one task. For this I use mutex.  
I use SPI to communicate with the SD card, plus I have to use an EEPROM that share the same SPI bus witht he SD card. First idea that came in my mind is to use one mutex to share the SPI bus between SD card and EEPROM and also between different tasks that write/read from the SD card.

Is there a better efficient way to manage my case ?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 12, 2015, 10:26am UTC](https://forums.freertos.org/t/best-practice-to-share-same-resources-between-tasks/1690/2 "2015-03-12T10:26:01Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Thursday, March 12, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/4d47b5cb/#336e):

Mutex is a good option for you. It will be obtained by the highest priority thread waiting on it, and has priority inheritance.

---

<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 12, 2015, 12:33pm UTC](https://forums.freertos.org/t/best-practice-to-share-same-resources-between-tasks/1690/3 "2015-03-12T12:33:03Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Thursday, March 12, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/4d47b5cb/#976f):

This sounds like the classic use of a mutex.

The minor nit is that it seems you are making public an implementation detail (the SD card and the EEPROM are on the same bus), but the actual mutex access can be put into the device driver APIs.

There could be some argument for having a second mutex outside the SPI bus mutex for just the SD card (or just the EEPROM) if the application layer needs to do multiple transactions that need to stay “atomic” (like reading a sector to update it). With just a single mutex, the task would need to lock out the EEPROM too during the update cycle.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 13, 2015, 10:21am UTC](https://forums.freertos.org/t/best-practice-to-share-same-resources-between-tasks/1690/4 "2015-03-13T10:21:55Z")

</div>

**[chaabanemalki](https://sourceforge.net/u/chaabanemalki/)** wrote on [Friday, March 13, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/4d47b5cb/#d650):

Thank you for the help
