Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Why can't I sleep correct time using gettimeofday()?  (Read 4684 times)

0 Members and 1 Guest are viewing this topic.

Stan Huang

    Topic Starter


    Beginner

    • Experience: Experienced
    • OS: Windows 7
    Why can't I sleep correct time using gettimeofday()?
    « on: December 03, 2013, 12:27:23 AM »
    Below is a function from my coworker. It runs well at X86. I ran it at iMX6 and it didn't work because the program usually slept just half or twice the time as I expected. The inaccuracy is too large. How come?

    inline void uusleep(int usecs) {
    struct timeval tpstart,tpend;

      gettimeofday(&tpstart,NULL);
      long ss = tpstart.tv_sec*1000000 + tpstart.tv_usec + usecs;

      while(1) {
       gettimeofday(&tpend,NULL);
       if( (tpend.tv_sec*1000000 + tpend.tv_usec) >= ss) break;
      }
    }