Linux下用 select 函数实现定时器(也可用作线程内延时)

来源:互联网 发布:识别文字的软件 编辑:程序博客网 时间:2024/04/28 03:38

Linux下用 select 函数实现定时器,也可用作线程内延时。

直接上源代码:

#include <stdio.h>  #include <sys/time.h>  int main()  {  struct timeval tv;  while(1){tv.tv_sec = 1;  // 定时1秒tv.tv_usec = 0;  switch(select(0, NULL, NULL, NULL, &tv)) {case -1:  // 错误printf("Error!\n");break;case 0: //超时printf("timeout expires.\n");break;default:printf("default\n");break;}}return 0;  }  

0 0
原创粉丝点击