我用的dev-c提示没有sleep函数,所以在网上找了个,但是太高端我不知道怎么用,请问大虾们怎么样。。。

小妮子驾到 2014-03-24 03:24:38

// --------------------------------------------------------------------------
// 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
}



...全文
316 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
小妮子驾到 2014-03-25
  • 打赏
  • 举报
回复
#define WIN32 #ifdef WIN32 如果定义了 WIN32 就。。。 。。。 #else 没有的话就。。。 。。。。 #endif 为什么我#define WIN32 会有那样的提示 而不定义就没有问题呢
小妮子驾到 2014-03-25
  • 打赏
  • 举报
回复

#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
小妮子驾到 2014-03-25
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
#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
谢谢赵老师
JiMoKuangXiangQu 2014-03-24
  • 打赏
  • 举报
回复
不用这么复杂吧,简单调用_sleep()就可以了。
赵4老师 2014-03-24
  • 打赏
  • 举报
回复
#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

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧