Пара вещей ..
Если вы хотите убедиться, что для завершения функции требуется время X, независимо от того, сколько времени потребовался фактический код в функции, выполните что-то вроде этого (очень псевдокод)
class Delay
{
public:
Delay(long long delay) : _delay(delay) // in microseconds
{
::gettimeofday(_start, NULL); // grab the start time...
}
~Delay()
{
struct timeval end;
::gettimeofday(end, NULL); // grab the end time
long long ts = _start.tv_sec * 1000000 + _start.tv_usec;
long long tse = end.tv_sec * 1000000 + end.tv_usec;
long long diff = tse - ts;
if (diff < _delay)
{
// need to sleep for the difference...
// do this using select;
// construct a struct timeval (same as required for gettimeofday)
fd_set rfds;
struct timeval tv;
int retval;
FD_ZERO(&rfds);
diff = _delay - diff; // calculate the time to sleep
tv.tv_sec = diff / 1000000;
tv.tv_usec = diff % 1000000;
retval = select(0, &rfds, NULL, NULL, &tv);
// should only get here when this times out...
}
}
private:
struct timeval _start;
};
Затем определите экземпляр этого класса Delay в верхней части вашей функции для отсрочки - это нужно сделать ... (этот код не проверен и может содержать ошибки, я просто набрал его, чтобы дать вам представление ...) )