QueryPerformanceCounter, QueryPerformanceFrequency를 이용하여
ms, us 단위의 Sleep 함수 구현하였다.
Sleep.zip
main.c
#include "Sleep.h"
#include <stdio.h>
void main()
{
int i=0;
if(InitSleep() == 0)
{
printf("Error\n");
return;
}
printf("Start");
USleep(1000000);
printf("Stop");
} |
Sleep.c
#include "Sleep.h"
static LARGE_INTEGER freq;
static long ufreq;
static long mfreq;
int InitSleep()
{
if (QueryPerformanceFrequency(&freq) == 0)
{
return 0;
}
ufreq = freq.QuadPart / 1000000;
mfreq = freq.QuadPart / 1000;
return 1;
}
void USleep(double us)
{
LARGE_INTEGER startTime;
LARGE_INTEGER nowTime;
memset(&startTime,0,sizeof(LARGE_INTEGER));
memset(&nowTime,0,sizeof(LARGE_INTEGER));
QueryPerformanceCounter(&startTime);
while (((nowTime.QuadPart - startTime.QuadPart) / ufreq) < us)
{
QueryPerformanceCounter(&nowTime);
}
}
void MSleep(double ms)
{
LARGE_INTEGER startTime;
LARGE_INTEGER nowTime;
memset(&startTime,0,sizeof(LARGE_INTEGER));
memset(&nowTime,0,sizeof(LARGE_INTEGER));
QueryPerformanceCounter(&startTime);
while (((nowTime.QuadPart - startTime.QuadPart) / mfreq) < ms)
{
QueryPerformanceCounter(&nowTime);
}
}
|
Sleep.h
#include <Windows.h>
int InitSleep();
void USleep(double us);
void MSleep(double ms); |