# Task won't run without taskYield() called from within it?

**URL:** <https://forums.freertos.org/t/task-wont-run-without-taskyield-called-from-within-it/5756>\
**Category:** Kernel\
**Created:** [December 17, 2013, 4:07pm UTC](https://forums.freertos.org/t/task-wont-run-without-taskyield-called-from-within-it/5756 "2013-12-17T16:07:21Z")\
**Posts on this page:** 5\
**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:** [December 17, 2013, 4:07pm UTC](https://forums.freertos.org/t/task-wont-run-without-taskyield-called-from-within-it/5756/1 "2013-12-17T16:07:21Z")

</div>

**[adamneilward](https://sourceforge.net/u/adamneilward/)** wrote on [Tuesday, December 17, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/fd5de240/#bd61):

Hi, I’m new to FreeRTOS and I am puzzled by something. I have 4 tasks, three of which are run using the vTaskDelayUntil() call to have them execute periodically (all three of these tasks works as expected). The 4th task however never gets called UNLESS I put a taskYield() inside it. All 4 tasks have the same priority.

static void task\_FFT(void \*pvParameters)  
{  
UNUSED(pvParameters);  
while(1)  
{  
task\_FFT\_DebugCounter++; //one of the other tasks prints this value out  
//taskYIELD(); //\<— commenting out this call seems to prevent this task from running  
}  
}

Since the first 3 tasks are periodic and mostly waiting, shouldn’t all the remaining runtime be used for the task\_FFT task? I have clearly misunderstood the workings of the scheduler but I’m not sure what I should be asking specifically.

Can someone please explain why task\_FFT() never gets any CPU time? The counter never gets incremented, showing that the function is not being scheduled.

The other 3 tasks are run every 1000ms, 500ms and 100ms respectively and I don’t believe they are using 100% cpu between them.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [December 17, 2013, 4:20pm UTC](https://forums.freertos.org/t/task-wont-run-without-taskyield-called-from-within-it/5756/2 "2013-12-17T16:20:14Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Tuesday, December 17, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/fd5de240/#7ae5):

What is the priority of the task? If it is \>0 then it should get all the time not used by your other tasks, if it is ==0 then it should share the time not used by your other tasks with the task.

Is task\_FFT\_DebugCounter declared volatile? If not the ++ can simply be optimized away as the value is never used.

Do you have configUSE\_PREEMPTION set to 1 in FreeRTOSConfig.h?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [December 18, 2013, 8:00am UTC](https://forums.freertos.org/t/task-wont-run-without-taskyield-called-from-within-it/5756/3 "2013-12-18T08:00:43Z")

</div>

**[adamneilward](https://sourceforge.net/u/adamneilward/)** wrote on [Wednesday, December 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/fd5de240/#38a0):

You were right about the volatile declaration. I was using 7 for all four tasks’ priorities and configUSE\_PREEMPTION is set to 1. Does seem to work correctly now without the taskYield call.

Oddly enough, even though all four tasks were using task\_???\_DebugCounter variables to indicate that they were all running, only the FFT task’s counter was being optimized out. In any case I’ve simply used the volatile constraint on all 4 timers now and everything is fine. I can throw in some other tasks now at different priorities to explore the system further.

The optimizer moves in mysterious ways.

Thanks for the help Dave, much appreciated.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [December 18, 2013, 5:04pm UTC](https://forums.freertos.org/t/task-wont-run-without-taskyield-called-from-within-it/5756/4 "2013-12-18T17:04:59Z")

</div>

**[jdurand](https://sourceforge.net/u/jdurand/)** wrote on [Wednesday, December 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/fd5de240/#e88f):

I’ve had the optimizer take out an array I was reading/writing in a function. I would have thought it would notice both reads and writes in the same place and leave it alone.

VOLATILE is becoming like LET in BASIC, use it everywhere.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [December 18, 2013, 9:24pm UTC](https://forums.freertos.org/t/task-wont-run-without-taskyield-called-from-within-it/5756/5 "2013-12-18T21:24:09Z")

</div>

**[adamneilward](https://sourceforge.net/u/adamneilward/)** wrote on [Wednesday, December 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/fd5de240/#44bd):

To be honest, I’ve been seeing weirdness in my code ever since I started playing with this scheduling system. That is not a criticism, I simply have not become accustomed so the ways of the FreeRTOS yet. In time it will come to me I’m sure. It is certainly a different way of thinking about embedded firmware.

I’m finding it rather fascinating and educational.
