1#include <stdio.h>
2#include <unistd.h>
3#include <sys/time.h>
4#include <stdint.h>
5
6int
7wait_a_while (useconds_t interval)
8{
9  int num_times = 0;
10  int return_value = 1;
11
12  struct timeval start_time;
13  gettimeofday(&start_time, NULL);
14  uint64_t target = start_time.tv_sec * 1000000 + start_time.tv_usec + interval;
15
16  while (1)
17    {
18      num_times++;
19      return_value = usleep (interval);
20      if (return_value != 0)
21        {
22          struct timeval now;
23          gettimeofday(&now, NULL);
24          interval = target - now.tv_sec * 1000000 + now.tv_usec;
25        }
26      else
27        break;
28    }
29  return num_times;
30}
31
32int
33main (int argc, char **argv)
34{
35  printf ("stop here in main.\n");
36  int num_times = wait_a_while (argc * 1000);
37  printf ("Done, took %d times.\n", num_times);
38
39  return 0;
40
41}
42