# How can i use the same delay before and after starting a thread

**URL:** <https://forums.freertos.org/t/how-can-i-use-the-same-delay-before-and-after-starting-a-thread/550>\
**Category:** Kernel\
**Created:** [July 28, 2015, 2:15am UTC](https://forums.freertos.org/t/how-can-i-use-the-same-delay-before-and-after-starting-a-thread/550 "2015-07-28T02:15:06Z")\
**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:** [July 28, 2015, 2:15am UTC](https://forums.freertos.org/t/how-can-i-use-the-same-delay-before-and-after-starting-a-thread/550/1 "2015-07-28T02:15:06Z")

</div>

**[cca1234](https://sourceforge.net/u/cca1234/)** wrote on [Tuesday, July 28, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/1e1e7172/#e854):

This may be a very stupid question but i am very new to RTOS and i haven’t been able to find where this is addressed. i have a function that i would like to be able to call either in the initialization before any threads are actually running or from a running thread (i.e., in the init section of main before the ostask scheduler starts as well as in a thread after the scheduler is running). HAL\_Delay won’t work properly after the thread starts and osDelay doesn’t seem to work before the thread and scheduler starts. is there a way to have a delay in a function so that function can be used regardless of where it is used?

for the moment i am just playing with the keil examples,e.g., FreeRTOS thread creation and moving delays around and trying different delays on an eval board. so i am stuck right out of the gates!

thanks

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [July 28, 2015, 5:28am UTC](https://forums.freertos.org/t/how-can-i-use-the-same-delay-before-and-after-starting-a-thread/550/2 "2015-07-28T05:28:27Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, July 28, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/1e1e7172/#5a37):

Neither HAL\_Delay or osDelay are FreeRTOS functions and I have no idea what they do, so I cannot comment on them.

After the scheduler has started you can use [vTaskDelay()](http://www.freertos.org/a00127.html) or [vTaskDelayUntil()](http://www.freertos.org/vtaskdelayuntil.html) - but they cannot be used before the scheduler starts.

You do do something like this:

```
if([xTaskGetSchedulerState()](http://www.freertos.org/a00021.html#xTaskGetSchedulerState)== taskSCHEDULER_NOT_STARTED )
{
    MyDelayFunction(); /* whatever that is */
}
else
{
    vTaskDelay();
}
```

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 28, 2015, 4:13pm UTC](https://forums.freertos.org/t/how-can-i-use-the-same-delay-before-and-after-starting-a-thread/550/3 "2015-07-28T16:13:56Z")

</div>

**[cca1234](https://sourceforge.net/u/cca1234/)** wrote on [Tuesday, July 28, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/1e1e7172/#88f8):

Thank you, this confirms my understanding (as limited as that may be at this point). The HAL\_Delay is part of the hardware abstraction layer for our processor (STM32 à la Keil) and the osDelay is part of the CMSIS library and probably others.

This is off-topic, but at this point we are trying to make somewhat of an informed decision between Keil RTX and OpenRTOS so if there is any sort of comparison documentation that exists it would be appreciated

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 29, 2015, 1:19pm UTC](https://forums.freertos.org/t/how-can-i-use-the-same-delay-before-and-after-starting-a-thread/550/4 "2015-07-29T13:19:34Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Wednesday, July 29, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/1e1e7172/#5ba7):

> if there is any sort of comparison documentation that exists it would be appreciated

They are both small OS’s solving the same problem. A lot more people use FreeRTOS, probably because its API is easier to use and you can use it with any compiler.
