James Wetzel 2011. 11. 4. 15:33
728x90
반응형

Sleep

The Sleep function suspends the execution of the current thread for a specified interval.

VOID Sleep(
  DWORD dwMilliseconds   // sleep time in milliseconds
);
 

Parameters

dwMilliseconds
Specifies the time, in milliseconds, for which to suspend execution. A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution. A value of INFINITE causes an infinite delay.

Return Values

This function does not return a value.

Remarks

A thread can relinquish the remainder of its time slice by calling this function with a sleep time of zero milliseconds.

You have to be careful when using Sleep and code that directly or indirectly creates windows. If a thread creates any windows, it must process messages. Message broadcasts are sent to all windows in the system. If you have a thread that uses Sleep with infinite delay, the system will deadlock. Two examples of code that indirectly creates windows are DDE and COM CoInitialize. Therefore, if you have a thread that creates windows, use MsgWaitForMultipleObjects or MsgWaitForMultipleObjectsEx, rather than Sleep.

QuickInfo

Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winbase.h.
Import Library: Use kernel32.lib.

See Also

Processes and Threads Overview, Process and Thread Functions, MsgWaitForMultipleObjects, MsgWaitForMultipleObjectsEx, SleepEx 


Sleep() 함수는 밀리초 단위 시간을 입력 받아 해당 프로그램을 '대기' 시키는 역활을 한다.
Sleep() 함수를 사용하면 "나는 잠시 쉬어도 되니 다른 프로그램에게 실행 기회를 주시오."라는 뜻을 운영체제에게 전달하는 것이다.
Sleep(0) 언뜻 보면 아무 의미가 없지만 실제로 "스위칭"이라는 중요한 효과를 낸다. 다시 말해 이 구문을 쓰는 순간 같은 우선 순위에 있는 다른 프로그램이 동작한다. "0초"의 시간만 지정되었으므로, 이보다 낮은 우선 순위의 프로그램에는 실행 기회가 주어지지 않을 것이다.

728x90
반응형