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

最近看到好多人在问,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);

}

就这么简单,拿去用吧,开发愉快!

猜你喜欢

转载自blog.csdn.net/guo_hongjun1611/article/details/40781533
今日推荐