WinAPI QueryPerformanceCounter 함수와 Stopwatch 클래스를 이용해 두가지 버젼으로 구현하였다.

 

 

 

 

usSleep.zip

 

 

 

 

QueryPerformanceCounter를 이용한 us Sleep

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Threading;
 
namespace Some
{
    public class Sleep
    {
        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
 
        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceFrequency(out long lpFrequency);
 
        private static long freq;
        private static double ufreq;
        private static double mfreq;
 
        static Sleep()
        {
            if (QueryPerformanceFrequency(out freq) == false)
            {
                throw new Win32Exception();
            }
            ufreq = freq / 1000000;
            mfreq = freq / 1000;
        }
 
        public Sleep()
        {
            USleep(0);
        }
 
        public void USleep(double us)
        {
            long startTime = 0;
            long nowTime = 0;
 
            QueryPerformanceCounter(out startTime);
 
            while (((nowTime - startTime) / ufreq) < us)
            {
                QueryPerformanceCounter(out nowTime);
            }
        }
 
        public void MSleep(double ms)
        {
            long startTime = 0;
            long nowTime = 0;
 
            QueryPerformanceCounter(out startTime);
 
            while (((nowTime - startTime) / mfreq) < ms)
            {
                QueryPerformanceCounter(out nowTime);
            }
        }
    }
}

 

 

 

 

 

Stopwatch Class를 이용한 us Sleep

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
 
namespace Some
{
    class SWSleep
    {
        long freq = 0;
        public SWSleep()
        {
            this.freq = Stopwatch.Frequency;
            USleep(0);
        }
 
        internal void USleep(int us)
        {
            double sec = (double)us / 1000000;
            Stopwatch sw = Stopwatch.StartNew();
            while (sw.ElapsedTicks / (double)freq < sec)
            {
            }
        }
 
        internal void MSleep(int ms)
        {
            double sec = (double)ms / 1000;
            Stopwatch sw = Stopwatch.StartNew();
            while (sw.ElapsedTicks / (double)freq < sec)
            {
            }
        }
    }
}

 

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);

 

 

 

 

 

 

구현 코드:

 

 

#include <stdio.h> #include <stdlib.h> #include <string.h> #define OUT_FILE_NAME "output.txt" #define INPUT_FILE_NAME "input.txt" void ReadInput(int *len, int *type); void WriteOutput(char *text); void RemoveOutput(); void ProcType1(int len); void ProcType2(int len); void ProcType3(int len); int main() { int len =0, type=0; void (*ProcTypeFn[4])(int); ProcTypeFn[1] = ProcType1; ProcTypeFn[2] = ProcType2; ProcTypeFn[3] = ProcType3;      RemoveOutput(); ReadInput(&len, &type); if((type >= 1 && type <= 3) && (len > 0 && len <=100)) { ProcTypeFn[type](len); } else { WriteOutput("INPUT ERROR!"); } return 0; } void ProcType1(int len) { //* //** //*** //**** //***** int i=0,j=0; for(i=1;i<=len;i++) { for(j=0;j<i;j++) { WriteOutput("*"); } WriteOutput("\n"); } } void ProcType2(int len) { //***** //**** //*** //** //* int i=0,j=0; for(i=len;i>0;i--) { for(j=0;j<i;j++) { WriteOutput("*"); } WriteOutput("\n"); } } void ProcType3(int len) { //    *    //4 //   ***   //3 //  *****  //2 // ******* //1 //********* int i=0,j=0,k=0; for(i=1;i<=len;i++) { for(j=len-i;j>0;j--) { WriteOutput(" "); } for(k=0;k<(i*2)-1;k++) { WriteOutput("*"); } WriteOutput("\n"); } } void RemoveOutput() { unlink(OUT_FILE_NAME); } void WriteOutput(char *text) { FILE* fs; char *res=0; fs=fopen(OUT_FILE_NAME,"a"); if(fs) { while(*text != '\0') { fwrite(text, 1, 1, fs); text++; } fclose(fs); } } void ReadInput(int *len, int *type) { FILE* fs; char data[100]={0}; char *res=0; fs=fopen(INPUT_FILE_NAME,"r"); if(fs) { fread(data, sizeof(char),5,fs); *len = atoi(strtok(data, " ")); *type = atoi(strtok(0, " ")); fclose(fs); } }

 

+ Recent posts