#ifndef __TNSTimer_H #define __TNSTimer_H /* NanoSeconds Timer ³ª³ë¼¼ÄÁ ŸÀ̸Ó(¾²·¹µå º£À̽º) ÄÄÇ»ÅÍ ¼º´É¿¡ µû¶ó ³ª³ëÃÊ(1/1000000000ÃÊ) ¼öÁرîÁö ŸÀÌ¸Ó ½ÇÇà °¡´É. WIn32ÀÇ Å¸À̸ӳª, VCLÀÇ TTimer¿¡ ºñÇØ Á¤È®µµ°¡ ³ôÀ¸¸ç ±âÁ¸ ŸÀ̸Ӱ¡ ÃÖ¼Ò 10~40ms Á¤µµÀÇ ÀÎÅ͹úÀ» °¡Áú¼ö ÀÖ´Â °Í¿¡ ºñÇØ ÀÌ ³ª³ë¼¼ÄÁ ŸÀ̸Ӵ ³ª³ë±Þ ½Ã°£±îÁöµµ °¡´ÉÇÏ´Ù. ¹°·Ð ÄÄÇ»ÅÍ ¼º´ÉÀÌ ¹ÞÃÄÁشٸé. ±âº» µ¿ÀÛÀº 1 MilliSeconds·Î µ¿ÀÛÇÑ´Ù. ÁÖÁö: ¾²·¹µå º£À̽º·Î µ¿ÀÛÇϸç, ¾²·¹µå ³»¿¡¼­ ·çÇÁ¸¦ µ¹¸ç CPUÀÇ µ¿ÀÛ ¼Óµµ¸¦ °è»êÇØ Å¸À̸Ӹ¦ µ¿ÀÛ½ÃÅ°´Âµ¥, MilliSeconds ÀÌÇÏ Å¸À̸ӿ¡¼­´Â CPU ºÎÇϸ¦ ¸¹ÀÌ ¾²´Â ´ÜÁ¡ÀÌ ÀÖ´Ù. ÀÌ´Â ¾²·¹µå°¡ Sleep ¾øÀÌ ·çÇÁ¸¦ µ¹±â ¶§¹®Àε¥, MilliSeconds ÀÌÇÏ Å¸ÀÌ¸Ó µ¿ÀÛ¿¡¼­´Â SleepÀ» ¾µ¼ö°¡ ¾ø´Ù. SleepÀÇ ±âº»ÀûÀÎ µ¿ÀÛÀº CPU°¡ º» ¾²·¹µå¿¡ È°´çµÈ Instruction µ¿ÀÛÀ» ¸ØÃß°í ´Ù¸¥ ÇÁ·Î¼¼½º¿¡ CPU¸¦ ¾çº¸ÇÏ´Â °ÍÀε¥, À̸¦ ContextSwitching À̶ó°í ÇÏ°í ÀÌ µ¿ÀÛÀÌ MilliSeconds ±ÞÀ¸·Î ÀÌ·ç¾îÁö±â ¶§¹®ÀÌ´Ù. Áï Çѹø ContextSwitching ÀÌ ÀϾ¼­ CPUÀÇ Á¦¾î±ÇÀ» ´Ù¸¥ ÇÁ·Î¼¼½º·Î ³Ñ°å´Ù°¡ ÀÌ ¾²·¹µå°¡ ´Ù½Ã ¹Þ±â±îÁö´Â ÃÖ¼Ò MilliSecondsÀÇ ½Ã°£À» ¼ÒºñÇϱ⠶§¹®¿¡ SleepÀ» ½á¼­´Â MilliSeconds ÀÌÇÏ Å¸À̸Ӹ¦ ¸¸µé¼ö ¾ø´Ù. ±×·¡¼­ MilliSeconds ÀÌÇÏ±Þ Å¸À̸ӿ¡¼­´Â Ç×»ó bUseSleep = false »óÅ¿¡¼­ µ¿ÀÛÇØ¾ß Çϸç, CPU ºÎÇϸ¦ ¸¹ÀÌ ¾²±â ¶§¹®¿¡, À̸¦ °í·ÁÇØ ÀûÁ¤ÇÑ ½Ã°£ µ¿¾È ¾²µµ·Ï ÇØ¾ß ÇÑ´Ù. CPU ºÎÇϸ¦ ¸¹ÀÌ ¾´´Ù°í Çߴµ¥, OS´Â ÇϳªÀÇ ÇÁ·Î¼¼½º¿¡ °úµµÇÑ CPU ºÎÇÏ°¡ °É¸®Áö ¾Ê°Ô ÀûÀýÇÏ°Ô ContextSwitchingÀ» Çϱ⠶§¹®¿¡, Áö³ªÄ¡°Ô ³ôÀº ¼öÁØÀ¸·Î ´Ù¸¥ ¾²·¹µåÀÇ µ¿ÀÛ¿¡ Å« ¿µÇâÀ» ÁÙ Á¤µµ·Î´Â Àý´ë ³ô¾ÆÁöÁö ¾Ê´Â´Ù. Millisecond = 1/1000ÃÊ Microsecond = 1/1000000ÃÊ Nanoseconds = 1/1000000000ÃÊ 1 Millisecond = 1000 Microseconds 1 Millisecond = 1000000 Nanoseconds Written by ±èżº. */ class TNSTimer : public TThread { private: int NanoSeconds; bool FEnabled; LARGE_INTEGER NSTime, NSFreq; private: double __fastcall GetMilliSeconds() { return NanoSeconds / 1000000.0; } void __fastcall SetMilliSeconds(double ms) { NanoSeconds = ms * 1000000.0; } public: typedef void (__closure *TNSTimerMethod)(); TNSTimerMethod NSTimerMethod; bool bUseSleep; public: __fastcall TNSTimer() : TThread(false) { NSTimerMethod = NULL; FEnabled = false; NanoSeconds = 1000000; // default´Â 1 MilliSeconds NSTime.QuadPart = 0; NSFreq.QuadPart = 0; bUseSleep = false; FreeOnTerminate = true; SetStdTime(); } void __fastcall Execute() { while(!Terminated) { if (NanoSeconds == 0 || !FEnabled) { Sleep(1); continue; } LARGE_INTEGER cur_ns; QueryPerformanceCounter(&cur_ns); LONGLONG between = cur_ns.QuadPart - NSTime.QuadPart; double second = (double)between / NSFreq.QuadPart; // ÀÌ·¸°Ô Çϸé ÃÊ·Î ³ª¿Â´Ù. if (second * 1000000 * 1000 < NanoSeconds) { if (bUseSleep) Sleep(0); continue; } NSTime = cur_ns; if (NSTimerMethod) NSTimerMethod(); } } void SetStdTime() { QueryPerformanceCounter(&NSTime); QueryPerformanceFrequency(&NSFreq); } // ³ª³ë¼¼ÄÁÀ¸·Î ÀÎÅ͹ú ÁöÁ¤, 1Àº ¹é¸¸ºÐÀÇ 1¹Ð¸®¼¼ÄÁ. 100000Àº 0.1 ¹Ð¸®¼¼ÄÁ. __property int NanoSecondsInterval = { read=NanoSeconds, write=NanoSeconds }; // -¹Ð¸®¼¼ÄÁÀ¸·Î ÀÎÅ͹ú ÁöÁ¤: 0.1 MiliSeconds, 0.01 MiliSeconds ½ÄÀ¸·Î ÁöÁ¤ °¡´É. __property double MilliSecondsInterval = { read=GetMilliSeconds, write=SetMilliSeconds }; // ŸÀÌ¸Ó µ¿ÀÛ on/off Á¦¾î __property bool Enabled = { read=FEnabled, write=FEnabled }; }; #endif