Jim Cownie | 18d8473 | 2014-05-10 17:02:09 +0000 | [diff] [blame] | 1 | #ifndef MY_SLEEP_H |
| 2 | #define MY_SLEEP_H |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include<stdlib.h> |
| 6 | #include<unistd.h> |
| 7 | |
| 8 | #include <sys/times.h> |
| 9 | #include <sys/time.h> |
| 10 | #include <time.h> |
| 11 | #include <errno.h> |
| 12 | |
| 13 | /*! Utility function to have a sleep function with better resolution and which only stops one thread. */ |
| 14 | |
| 15 | static void my_sleep(double sleeptime){ |
| 16 | struct timeval tv; |
| 17 | struct timezone tzp; |
| 18 | double start; |
| 19 | double real; |
| 20 | if(gettimeofday(&tv,&tzp)!=0) { |
| 21 | perror("get_time: "); |
| 22 | exit(-1); |
| 23 | } |
| 24 | start = (double)tv.tv_sec + ((double)tv.tv_usec/1000000.0); |
| 25 | real=start; |
| 26 | while( (real-start)<sleeptime){ |
| 27 | if(gettimeofday(&tv,&tzp)!=0) { |
| 28 | perror("get_time: "); |
| 29 | exit(-1); |
| 30 | } |
| 31 | real = (double)tv.tv_sec + ((double)tv.tv_usec/1000000.0); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | #endif |