From e050ccb9de7fd9607e51cddb6bc22510eec16679 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 18 Oct 2021 14:23:53 +0200 Subject: [PATCH] Bugfix: Yield after every execution of 'loop' if a loop delay of 0 is specified. Otherwise we experience ressource starvation with the first started thread hogging the CPU all the time and the other threads will be never served. --- src/Arduino_Threads.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Arduino_Threads.cpp b/src/Arduino_Threads.cpp index 444ded5..89a6181 100644 --- a/src/Arduino_Threads.cpp +++ b/src/Arduino_Threads.cpp @@ -118,8 +118,17 @@ void Arduino_Threads::threadFunc() } } - /* Sleep for the time we've been asked to insert between loops. - */ - rtos::ThisThread::sleep_for(rtos::Kernel::Clock::duration_u32(_loop_delay_ms)); + if (_loop_delay_ms) { + /* Sleep for the time we've been asked to insert between loops. + */ + rtos::ThisThread::sleep_for(rtos::Kernel::Clock::duration_u32(_loop_delay_ms)); + } + else + { + /* In any case yield here so that other threads can also be + * executed following the round-robin scheduling paradigm. + */ + rtos::ThisThread::yield(); + } } }