Comments on: ESP32 FreeRTOS Mutex – Getting Started (Arduino IDE) https://randomnerdtutorials.com/esp32-freertos-mutex-arduino/ Learn ESP8266, ESP32, Arduino, and Raspberry Pi Tue, 20 Jan 2026 12:00:35 +0000 hourly 1 https://wordpress.org/?v=6.8.5 By: David Lowther https://randomnerdtutorials.com/esp32-freertos-mutex-arduino/#comment-1153126 Tue, 20 Jan 2026 12:00:35 +0000 https://randomnerdtutorials.com/?p=181013#comment-1153126 In reply to David Lowther.

There is also the hypothetical issue of the non-atomic nature of :
if (a) {
a=false;
In theory one task could do the ‘if(a)’ then be descheduled.
Then the other task would run and also see that ‘a’ is true.
So now both tasks think they ‘own’ the serial port.
That’s unlikely to happen, but if you complex code with many instances of this type of non-atomic code, then you might start to encounter occasional, hard to explain, unexpected behaviour.

]]>
By: David Lowther https://randomnerdtutorials.com/esp32-freertos-mutex-arduino/#comment-1153122 Tue, 20 Jan 2026 11:52:06 +0000 https://randomnerdtutorials.com/?p=181013#comment-1153122 In your code “boolean a = true;” that should be declared with the voltaile attribute. Without it the compiler may optimise the compiled code so that each task has its own copy of ‘a’ in a register at some points of the task’s code. So if you were unlucky it wouldn’t work as you intended.

Other than that, a quick look over your code suggests to me that your home made mutual exclusion would work. However, using a mutex would stop one task from running when the other task was using the serial output. Whereas in your code both tasks would always be eligible for being run by the scheduler, which would waste some processing power.

I’d say, in summary, that your solution would be fine (with the addition of volatile for ‘a’) for this simple case. For more complex code it would be better, and make the purpose of the code more obvious, if you used the facilities provided by FreeRTOS to manage scheduling and mutual exclusion.

]]>
By: Richard https://randomnerdtutorials.com/esp32-freertos-mutex-arduino/#comment-1153110 Tue, 20 Jan 2026 11:38:29 +0000 https://randomnerdtutorials.com/?p=181013#comment-1153110 Please what is different between this and between this:

boolean a = true;

void Task1(void *parameter) {
for (;;) {
if (a) {
a=false;
Serial.println(“Task1: Logging from Task 1”);
vTaskDelay(1000 / portTICK_PERIOD_MS);
Serial.println(“Task1: End of log from Task 1”);
a=true;
}
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
}

void Task2(void *parameter) {
for (;;) {
if (a) {
a=false;
Serial.println(“Task2: Logging from Task 2”);
vTaskDelay(800 / portTICK_PERIOD_MS);
Serial.println(“Task2: End of log from Task 2”);
a=true;
}
vTaskDelay(3000 / portTICK_PERIOD_MS);
}
}

void setup() {
Serial.begin(115200);
delay(1000);
Serial.println(“Starting FreeRTOS”);

xTaskCreatePinnedToCore(
Task1, // Task function
“Task1”, // Task name
3000, // Stack size
NULL, // Task parameters
1, // Priority
NULL, // Task handle
1 // Core ID
);

xTaskCreatePinnedToCore(
Task2, // Task function
“Task2”, // Task name
3000, // Stack size
NULL, // Task parameters
2, // Higher priority
NULL, // Task handle
1 // Core ID
);
}

void loop() {

}

]]>
By: Bernard Barrois https://randomnerdtutorials.com/esp32-freertos-mutex-arduino/#comment-1152804 Mon, 19 Jan 2026 06:41:59 +0000 https://randomnerdtutorials.com/?p=181013#comment-1152804 Oui, ça manquait !
Merci

]]>
By: Sara Santos https://randomnerdtutorials.com/esp32-freertos-mutex-arduino/#comment-1150970 Thu, 15 Jan 2026 20:23:45 +0000 https://randomnerdtutorials.com/?p=181013#comment-1150970 In reply to David Lowther.

Hi.
Yes, that’s a great application for FreeRTOS.

Regards,
Sara

]]>
By: David Lowther https://randomnerdtutorials.com/esp32-freertos-mutex-arduino/#comment-1150949 Thu, 15 Jan 2026 18:12:27 +0000 https://randomnerdtutorials.com/?p=181013#comment-1150949 Another place it’s worth having mutual exclusion is between call backs e.g. from the async web server and the code running in loop. I had some mysterious crashes before I added it. I didn’t actually use a mutex for this. What I did do was use a FreeRTOS queue. The call backs write the call back info to that queue, then loop reads the queue in a non-blocking manner.

]]>
By: Tony Contrada https://randomnerdtutorials.com/esp32-freertos-mutex-arduino/#comment-1150918 Thu, 15 Jan 2026 15:36:12 +0000 https://randomnerdtutorials.com/?p=181013#comment-1150918 Thank you for the Mutex Example

]]>