Couple of things..
If you want to ensure that the function takes a Time X to complete, irrespective of how long the actual code within the function took, do something like this (highly pseudo code)
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;
};
Then define an instance of this Delay class at the top of your function to delay - should do the trick... (this code is untested and could have bugs in it, I just typed it to give you an idea..)