当前位置:   article > 正文

linux 用 select 替代 sleep(微秒级)_select来模拟sleep

select来模拟sleep
  1. #include <sys/select.h>
  2. #include <sys/time.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. void local_sleep (long sec, long usec) {
  7. struct timeval timeout = {sec, usec};
  8. int ret = 0;
  9. if ((0 == timeout.tv_sec) || (timeout.tv_usec < 20))
  10. {
  11. printf("local sleep error! input sleep time must greater than 20ms !\n");
  12. timeout.tv_usec = 20;
  13. }
  14. ret = select(0, NULL, NULL, NULL, &timeout);
  15. if ((-1 == ret) || (ret))
  16. {
  17. printf("local sleep error!\n");
  18. }
  19. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/225082?site
推荐阅读
相关标签
  

闽ICP备14008679号