# Writing and Reading FreeRTOS Queue from Different ESP32 Cores

**URL:** <https://forum.arduino.cc/t/writing-and-reading-freertos-queue-from-different-esp32-cores/688431>\
**Category:** Programming\
**Created:** [December 25, 2020, 12:55am UTC](https://forum.arduino.cc/t/writing-and-reading-freertos-queue-from-different-esp32-cores/688431 "2020-12-25T00:55:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [December 25, 2020, 12:55am UTC](https://forum.arduino.cc/t/writing-and-reading-freertos-queue-from-different-esp32-cores/688431/1 "2020-12-25T00:55:03Z")

</div>

So, if the only queue-writing task is running on Core 0 and the only queue-reading task is running on Core 1, must the queue be locked with a common mutex while each of these tasks is doing its thing?

Or, since only one task is writing and only one task is reading (even though on independently-running cores), there won't be a conflict and mutex-locking isn't required?

Thanks.

---

<div class="post-metadata">

**Author:** ![PieterP](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@PieterP](https://forum.arduino.cc/u/PieterP)\
**Post date:** [December 25, 2020, 2:45am UTC](https://forum.arduino.cc/t/writing-and-reading-freertos-queue-from-different-esp32-cores/688431/2 "2020-12-25T02:45:51Z")

</div>

FreeRTOS queues are synchronized by the kernel, you don't need any mutexes in your own code.

See the example in the documentation: [FreeRTOS - Open Source Software for Embedded Systems - FreeRTOS xQueueReceive() API function description and example](https://www.freertos.org/a00118.html)

I couldn't find an explicit comment in the docs that you don't need mutexes, but this thread answers it, for example: [Question: Safe to send structures to a queue from multiple tasks without mutex? - FreeRTOS](https://www.freertos.org/FreeRTOS_Support_Forum_Archive/October_2016/freertos_Question_Safe_to_send_structures_to_a_queue_from_multiple_tasks_without_mutex_6a1a615fj.html)

Note that having one task that reads and another that writes in general _does_ require some form of synchronization, if not locks or mutexes then at least atomic variables.  
In this case, the FreeRTOS kernel just handles it for you.

Pieter

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [December 25, 2020, 3:24am UTC](https://forum.arduino.cc/t/writing-and-reading-freertos-queue-from-different-esp32-cores/688431/3 "2020-12-25T03:24:04Z")

</div>

OK, thanks. That's pretty much what I figured. My only doubt was because the "vanilla FreeRTOS" documentation seems to assume a single-core environment while ESP32 has two cores. Good to know that the ESP32 port takes this into account with regard to queue management.

---

<div class="post-metadata">

**Author:** ![PieterP](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@PieterP](https://forum.arduino.cc/u/PieterP)\
**Post date:** [December 25, 2020, 12:00pm UTC](https://forum.arduino.cc/t/writing-and-reading-freertos-queue-from-different-esp32-cores/688431/4 "2020-12-25T12:00:24Z")

</div>

The ESP-IDF docs seem to use the same examples: [FreeRTOS (Overview) - ESP32 - — ESP-IDF Programming Guide latest documentation](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/freertos.html#_CPPv413xQueueReceive13QueueHandle_tPCv10TickType_t)

It would indeed be a surprise if the Queue API would behave differently on ESP32, but I agree that it would be nice if the docs were more explicit about it.

Looking at the sources for the Queue send function, it looks like it does indeed use a mutex and enters a critical section: [esp-idf/queue.c at 178b122c145c19e94ac896197a3a4a9d379cd618 · espressif/esp-idf · GitHub](https://github.com/espressif/esp-idf/blob/178b122c145c19e94ac896197a3a4a9d379cd618/components/freertos/queue.c#L784)

Synchronization in a single-core system with preemptive scheduling isn't all that different from a multiprocessor environment, in both cases, you need mutexes/critical sections and atomic variables (although you don't need actual memory barriers on a single core, you still need to prevent the compiler from reordering your reads/writes to shared data). There are of course some differences, especially if you're writing the kernel/scheduler, but from user space it doesn't matter too much in most cases.

A final remark: given that the FreeRTOS Queue uses locks to allow any number of threads to access the queue at the same time, you might want to use a lock-free alternative if you have a single producer and a single consumer. This is important if performance of the queue is an issue, locks can slow things down significantly. You could Google for "SPSC queue" or "lock-free queue", for example. IIRC, Boost has one, and so does Folly.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:54am UTC](https://forum.arduino.cc/t/writing-and-reading-freertos-queue-from-different-esp32-cores/688431/5 "2021-05-06T00:54:54Z")

</div>

Closed on May 6, 2021, 12:54am UTC


