69,369
社区成员
发帖
与我相关
我的任务
分享
// --------------------------------------------------------------------------
// Sleep for a number of milliseconds
void zclock_sleep(int msecs){
#if defined (__UNIX__)
struct timespec t;
t.tv_sec = msecs / 1000;
t.tv_nsec = (msecs % 1000) * 1000000;
nanosleep (&t, NULL);
#elif ()
// Windows XP/2000: 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. This
// behavior changed starting with Windows Server 2003.
# if defined (NTDDI_VERSION) && defined (NTDDI_WS03) && (NTDDI_VERSION >= NTDDI_WS03)
Sleep (msecs);
# else
if (msecs > 0)
Sleep (msecs);
# endif
#endif
}
上面这段函数是网上找的实现sleep
我用google翻译注释的内容为
//Windows XP/2000:零值会导致线程放弃 它的时间
//余数为相同优先级的其他线程
//准备运行。如果有相同优先级的就绪运行没有其他线程,
//该函数立即返回,线程继续执行。这
//行为改变开始与Windows Server 2003。
#if defined (__UNIX__)
判断UNIX
# if defined (NTDDI_VERSION) && defined (NTDDI_WS03) && (NTDDI_VERSION >= NTDDI_WS03)
判断WIN?
__UNIX__ NTDDI_VERSION NTDDI_WS03
这3个我应该去定义为什么
我是新手刚请各位大虾帮帮
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
printf("sleep的效果");
zclock_sleep(5000);//5秒
system("pause");
return 0;
}
// --------------------------------------------------------------------------
// Sleep for a number of milliseconds
void zclock_sleep(int msecs){
#if defined (__UNIX__)
struct timespec t;
t.tv_sec = msecs / 1000;
t.tv_nsec = (msecs % 1000) * 1000000;
nanosleep (&t, NULL);
#elif ()
// Windows XP/2000: 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. This
// behavior changed starting with Windows Server 2003.
# if defined (NTDDI_VERSION) && defined (NTDDI_WS03) && (NTDDI_VERSION >= NTDDI_WS03)
Sleep (msecs);
# else
if (msecs > 0)
Sleep (msecs);
# endif
#endif
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#define WIN32
我这里还有个一问题如果加了这句我定义为WIN 才会进入
#ifdef WIN32
#include <windows.h>
void sleep_ms(int ms) {
printf("你进入的是win");
Sleep(ms);// Sleep以毫秒为单位
}
但是我定义了后DEV-C++会出现
sleep.c [Warning] "WIN32" redefined
<built-in> [Warning] this is the location of the previous definition
的提示
void sleep_ms(int ms);
int main( void )
{
sleep_ms(5000);
printf("LOOK");
system("pause");
return 0;
}
#ifdef WIN32
#include <windows.h>
void sleep_ms(int ms) {
printf("你进入的是win");
Sleep(ms);// Sleep以毫秒为单位
}
#else
#include <unistd.h>
void sleep_ms(int ms) {
printf("你进入的是linux");
usleep(ms*1000);//usleep单位是微秒us*1000毫秒
}
#endif
#ifdef WIN32
#include <windows.h>
void sleep_ms(int ms) {
Sleep(ms);
}
#else
#include <unistd.h>
void sleep_ms(int ms) {
usleep(ms*1000);
}
#endif