天天看點

定時器 通過select設定定時器

#include <sys/time.h>    
#include <sys/select.h>    
#include <time.h>    
#include <stdio.h> 
//秒級定時器
void setTimer(int seconds,int mseconds)
{
  struct timeval temp;
  temp.tv_sec=seconds;
  temp.tv_usec=mseconds;

  select(0,NULL,NULL,NULL,&temp);
  printf("Hello World\n");

  return ;
}

int main()
{
  int i;
  for(i=0;i<100;i++)
  {
    setTimer(3,0);
    printf("######\n");
    return 0;
  }
}      
void milliseconds_sleep(unsigned long mSec){
     struct timeval tv;
     tv.tv_sec=mSec/1000;
     tv.tv_usec=(mSec%1000)*1000;
     int err;
     do{
        err=select(0,NULL,NULL,NULL,&tv);
     }while(err<0 && errno==EINTR);
 }