# vTaskDelay in Windows 7

**URL:** <https://forums.freertos.org/t/vtaskdelay-in-windows-7/320>\
**Category:** Kernel\
**Created:** [September 5, 2011, 11:21am UTC](https://forums.freertos.org/t/vtaskdelay-in-windows-7/320 "2011-09-05T11:21:03Z")\
**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:** [September 5, 2011, 11:21am UTC](https://forums.freertos.org/t/vtaskdelay-in-windows-7/320/1 "2011-09-05T11:21:03Z")

</div>

**[mikete](https://sourceforge.net/u/mikete/)** wrote on [Monday, September 05, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/087e307d/#e31f):

Hello,  
Using port to windows i was wondered that vTaskDelay not work correctly, i.e. for example vTaskDelay(100) will delay task on 2000ms. Is it problem only for windows 7 and how solve that?  
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:** [September 5, 2011, 12:10pm UTC](https://forums.freertos.org/t/vtaskdelay-in-windows-7/320/2 "2011-09-05T12:10:51Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, September 05, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/087e307d/#f060):

The Windows port is a FreeRTOS “Simulator”.&nbsp; As it says on the documentation page for the port “The tick interrupt generation is simulated by a high priority Windows thread that will periodically pre-empt the low priority threads that are running tasks. The tick rate achievable is limited by the Windows system clock, which in normal FreeRTOS terms is very slow and has a very low precision. True real time behaviour cannot therefore be obtained.”

While it is common to have ticks running at 100Hz or even 1KHz on a microcontroller, the Windows clock is in comparison extremely clunky.&nbsp; The resolution is dependent on your hardware, but often in the 40ms range (with huge jitter too).&nbsp; Therefore, attempting to use this clock to get near real time performance would prevent any tasks being able to accurately block for any time that was not a multiple of this 40 (ish) ms, and the average accuracy would be 40/2 ms - not very good.

For those reasons, the port simulates a real time system by making time run slowly, where it pretends every ‘n’ ms is 1 ms.&nbsp; Asking for a 100ms delay should always result in approximately the same delay being achieved, but it will be nothing like 100ms.

The simulator is a convenient place to develop code, that can then be moved relatively easy onto a real target, it is not in itself intended to be a real time system.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 6, 2011, 9:24am UTC](https://forums.freertos.org/t/vtaskdelay-in-windows-7/320/3 "2011-09-06T09:24:25Z")

</div>

**[mikete](https://sourceforge.net/u/mikete/)** wrote on [Tuesday, September 06, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/087e307d/#252c):

Ok, i understood. Also i looked at freertos process cpu loading and it was always above 25%. In my mind all of this restrictions make no sense to using windows port. I supposed that windows port of freertos is just wrapper above winapi functions and not realize freertos algorithms. Earlier i wrote own wrappers for threads, seamphores etc (in windows it wrap winapi in device it wrap freertos api) but when i saw&nbsp; freertos windows port i thought that can use just freertos api on both platforms without any wrappers.  
Thanks anyway.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 6, 2011, 9:27am UTC](https://forums.freertos.org/t/vtaskdelay-in-windows-7/320/4 "2011-09-06T09:27:48Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Tuesday, September 06, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/087e307d/#cc87):

Read the documentation page for the project.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 19, 2011, 5:01pm UTC](https://forums.freertos.org/t/vtaskdelay-in-windows-7/320/5 "2011-09-19T17:01:07Z")

</div>

**[arainho](https://sourceforge.net/u/arainho/)** wrote on [Monday, September 19, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/087e307d/#50f1):

HI All,

Actually windows can generate clock ticks of 1000 or more with no problems under windows. Windows does have special counters that can be used to generate these high tick clocks. For those interrested below is my version of the “prvSimulatedPeripheralTimer” routine which creates the clock ticks. Just substitute the current one in port.c and start ticking away.&nbsp; In my PC the reported max frequency is of about 2mhz!!!

I have not tested this on single core computers, but on twin core one core is used at 100% and the other is left free which gives the system good response.

Regards,

Alfredo

//--------------------------------------------------------------------------------------------  
// Modification by: Alfredo Rainho Neves  
// This is an updated version that will actually responde a few thousand times  
// a second.  
// The CPU load will show very high, but the routine actualy yield  
// giving other tasks a chance. In a multicore CPU only one core will have  
// a 100% CPU usage  
static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )  
{  
LARGE\_INTEGER lpFrequency;  
LARGE\_INTEGER startTime;  
LARGE\_INTEGER currentTime;  
unsigned long&nbsp; countPerTick;  
unsigned long&nbsp; elapsed;

/\* Just to prevent compiler warnings. \*/  
( void ) lpParameter;

// Get the performance counter frequency  
QueryPerformanceFrequency(&lpFrequency);

// Calculate the count per ticks  
countPerTick = lpFrequency.LowPart / configTICK\_RATE\_HZ;

while(1)&nbsp; {  
// Get the start time  
QueryPerformanceCounter(&startTime);

// Wait for time period to elapse  
do {  
// Yield to another task to make the system more responsive  
Sleep(0);

// Get the current time  
QueryPerformanceCounter(&currentTime);

// Calculate the elpased time  
elapsed = currentTime.LowPart - startTime.LowPart;

} while (elapsed \< countPerTick);

WaitForSingleObject( pvInterruptEventMutex, INFINITE );

/\* The timer has expired, generate the simulated tick event. \*/  
ulPendingInterrupts |= ( 1 \<\< portINTERRUPT\_TICK );

/\* The interrupt is now pending - notify the simulated interrupt  
handler thread. \*/  
SetEvent( pvInterruptEvent );

/\* Give back the mutex so the simulated interrupt handler unblocks  
and can access the interrupt handler variables. \*/  
ReleaseMutex( pvInterruptEventMutex );  
}

#ifdef \_\_GNUC\_\_  
/\* Should never reach here - MingW complains if you leave this line out,  
MSVC complains if you put it in. \*/  
return 0;  
#endif  
}
