# taskYIELD() in a loop as best strategy?

**URL:** <https://forums.freertos.org/t/taskyield-in-a-loop-as-best-strategy/12664>\
**Category:** Kernel\
**Created:** [May 24, 2021, 9:59pm UTC](https://forums.freertos.org/t/taskyield-in-a-loop-as-best-strategy/12664 "2021-05-24T21:59:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rik001](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rik001](https://forums.freertos.org/u/Rik001)\
**Post date:** [May 24, 2021, 9:59pm UTC](https://forums.freertos.org/t/taskyield-in-a-loop-as-best-strategy/12664/1 "2021-05-24T21:59:00Z")

</div>

Hi,  
I have a loop inside which a flag is constantly monitored. Because the tick is 1ms and I am ok checking for that flag every few ms, I don’t want the execution to waste time for the rest of that tick period inside that loop when other taks could do more productive/important work.

Is taskYIELD() the best strategy? like this:

```
while (flag == 1)
{
    taskYIELD();
}
... rest of code

```

or perhaps putting a vTaskDelay of few ticks or maybe some other option?

Thank you 🙂

---

<div class="post-metadata">

**Author:** ![aws-archit](https://avatars.discourse-cdn.com/v4/letter/a/ccd318/32.png) [@aws-archit](https://forums.freertos.org/u/aws-archit)\
**Post date:** [May 25, 2021, 12:59am UTC](https://forums.freertos.org/t/taskyield-in-a-loop-as-best-strategy/12664/2 "2021-05-25T00:59:17Z")

</div>

Hi @Rik001, is the flag you’re polling being set to indicate/notify an event/state from another task?  
In that case, it would be better to block this task until it receives the notification from the other task using the mechanism of [FreeRTOS Task Notifications](https://www.freertos.org/RTOS-task-notifications.html) instead of periodically polling for the flag.

---

<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:** [May 25, 2021, 1:47am UTC](https://forums.freertos.org/t/taskyield-in-a-loop-as-best-strategy/12664/3 "2021-05-25T01:47:35Z")

</div>

taskYIELD() likely doesn’t do what you want, unless your task is a priority 0 (i.e. IDLE Priority) task, as a yield will only let another task of your priority or higher run. (and only goes to a higher priority if you have disabled preemption, or it would have gone to that when it first became ready).

---

<div class="post-metadata">

**Author:** ![Rik001](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rik001](https://forums.freertos.org/u/Rik001)\
**Post date:** [May 25, 2021, 4:36pm UTC](https://forums.freertos.org/t/taskyield-in-a-loop-as-best-strategy/12664/4 "2021-05-25T16:36:05Z")

</div>

Thank you aws-archir and Richard,

@aws-archit, no, the flag is just a global variable that can be set by an INT. What would you suggest for this case as an elegant/correct solution?

@richard-damon, all the tasks are currently at the same priority. That is why I thought of that solution. But perhaps there are better ones?

Thank you 🙂

---

<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:** [May 25, 2021, 5:22pm UTC](https://forums.freertos.org/t/taskyield-in-a-loop-as-best-strategy/12664/5 "2021-05-25T17:22:55Z")

</div>

All tasks being the same priority normally means that you didn’t need an RTOS.

Better would be to use a FreeRTOS operation. It could be an EventGroup, Semaphore or a direct-to-task notification depending on exactly what you want to do with it.

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [May 25, 2021, 7:35pm UTC](https://forums.freertos.org/t/taskyield-in-a-loop-as-best-strategy/12664/6 "2021-05-25T19:35:56Z")

</div>

> [@Rik001](#):
>
> Hi,  
> I have a loop inside which a flag is constantly monitored. Because the tick is 1ms and I am ok checking for that flag every few ms, I don’t want the execution to waste time for the rest of that tick period inside that loop when other taks could do more productive/important work.
> 
> Is taskYIELD() the best strategy? like this:
> 
> ```auto
> while (flag == 1)
> {
> taskYIELD();
> }
> ... rest of code
> 
> ```
> 
> or perhaps putting a vTaskDelay of few ticks or maybe some other option?

well, it mostly depends on what “rest of code” does. If that’s a predictably bound computation, you can simply create a FreeRTOS timer that checks the flag and executes “rest of code” if flags is != 1. If that is a lengthy computation, however, you can employ a variation of your scheme, namely, a task that is normally suspended and woken via vTaskResume() by your timer routine if the flag is != 1. That’s fairly off-the-shelf-code.

---

<div class="post-metadata">

**Author:** ![Rik001](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rik001](https://forums.freertos.org/u/Rik001)\
**Post date:** [May 25, 2021, 10:06pm UTC](https://forums.freertos.org/t/taskyield-in-a-loop-as-best-strategy/12664/7 "2021-05-25T22:06:47Z")

</div>

Thank you RAc and Richard 🙂
