# Knowing Scheduler Status

**URL:** <https://forums.freertos.org/t/knowing-scheduler-status/1326>\
**Category:** Kernel\
**Created:** [February 21, 2007, 10:22pm UTC](https://forums.freertos.org/t/knowing-scheduler-status/1326 "2007-02-21T22:22:22Z")\
**Posts on this page:** 4\
**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:** [February 21, 2007, 10:22pm UTC](https://forums.freertos.org/t/knowing-scheduler-status/1326/1 "2007-02-21T22:22:22Z")

</div>

**nobody** wrote on [Wednesday, February 21, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/39c96c6d/#6153):

Hi,  
Initializing and using LCD requires a lot of software delays, this is inefficient. Better way is to replace every SoftwareDelay() with vTaskDelay(t) or vTaskDelayUntil(t).

However vTaskDelay() can only be used when the scheduler is running.

I want to write a function Delay() such that, when it’s called when scheduler is not running, it uses software delay, otherwise it uses vTaskDelay().

If [scheduler is NOT running]  
&nbsp;&nbsp; Delay(1000000);  
else  
&nbsp;&nbsp; vTaskDelay(300);

How can I know scheduler is running or not? The variable "xSchedulerRunning" is declared in task.c, so not accessible. Should I take xSchedulerRunning out and declare in task.h, or is there a cleaner way?

Thank you.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 21, 2007, 10:36pm UTC](https://forums.freertos.org/t/knowing-scheduler-status/1326/2 "2007-02-21T22:36:09Z")

</div>

**nobody** wrote on [Wednesday, February 21, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/39c96c6d/#6153/396c):

A couple suggestions:

1) Rather than make the variable global, include an access function in tasks.c such as xIsSchedulerRunning() that returns the variable.&nbsp; This is a bit neater.

2) Most ports use a variable called ulCriticalNesting that is initialized to non zero, then set to zero when the first task starts.&nbsp;&nbsp;&nbsp; It is not static so can be accessed from any file, but be careful as it will still be non zero from within a critical section even when the scheduler is running.

Dave.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 21, 2007, 10:38pm UTC](https://forums.freertos.org/t/knowing-scheduler-status/1326/3 "2007-02-21T22:38:09Z")

</div>

**nobody** wrote on [Wednesday, February 21, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/39c96c6d/#6153/4c4f):

It is a fairly safe bet that if uxCriticalNesting (ulCriticalNesting?) is \> 15 then the scheduler has not been started unless you are writing really screwy code.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 27, 2007, 12:48am UTC](https://forums.freertos.org/t/knowing-scheduler-status/1326/4 "2007-02-27T00:48:11Z")

</div>

**nobody** wrote on [Tuesday, February 27, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/39c96c6d/#6153/e5aa):

Thx… i tried the first suggestion, it works… , and clean code too…

I didnt try the second suggestion because I just dont understand what ulCriticalNesting does. Thank you anyway
