문제

i've found this piece of code and tested it.

#include <time.h>

int main ()
{
  time_t start_time;
  time_t current_time;
  int TIMEOUT=5;
  start_time = time(NULL);
  current_time = time(NULL);

  while (current_time < start_time + TIMEOUT)
  {
  //do everything here
  current_time = time(NULL);
  }

}

It works like a charm, but it takes completely one cpu off. Is there a way to don't waste so much cpu? Thanks

Edit, I can't use sleep, i must do other thing during waiting

도움이 되었습니까?

해결책

You need to use a timer. There are several APIs to do this.

Just using alarm is the simpliest.

There is setitimer/getitimer.

There is the timer_create/timer_destroy/timer_settime family.

Each has their own strengths offset by their own complexities.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top