c/c++中sleep()函数毫秒级的实现

时间:2014-11-04 13:17:06   收藏:0   阅读:297

最近看到好多人在问,c/c++中的sleep函数是秒级的,能不能实现毫秒级的呢?当然很简单,我的写法如下

#include <stdio.h>

#include <sys/select.h>



static void sleep_ms(unsigned int secs)

{

    struct timeval tval;

    tval.tv_sec=secs/1000;

    tval.tv_usec=(secs*1000)%1000000;

    select(0,NULL,NULL,NULL,&tval);

}

就这么简单,拿去用吧,开发愉快!
评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!