how long can a variable of clock_t be?

is it a long int? Also, if I wait for a 10 seconds, and then ask 2 different threads to store clock() in a variable at the exact same time, will those two variables be the same?

The reason I'm asking is because I'm building a neural network in which some communities (classes) are to be penalised for their age (measured in wall time). I don't want to use time() because many communities are only fractions of seconds old. But I need to be sure that a) if my code runs for several days or even weeks, that the clock_t variable is not going to loop and b) that two separate threads asking for clock() are going to get the same value providing they were called at the same instant.

Thanks :)
in answer to a):

1
2
3
cout << CLOCKS_PER_SEC << endl;
cout << numeric_limits<clock_t>::max() << endl;
cout << numeric_limits<clock_t>::max() / CLOCKS_PER_SEC << endl;


returns:

1000
2147483647
2147483


So basically it's a signed integer in my case. This works out to 24 days, 20 hours, 31 minutes, 23 seconds and 647 milliseconds. What happens to the function when I reach this value? Does clock() revert to 0 or -2147483647?
Last edited on
In time.h of my compiler (VS2010) I have the following:

typedef long clock_t

This means that it is a 32bit integer which will represent at most 4,294,967,296 clock ticks. This means that at an iteration rate of, say, 60Hz, you can only run for 71,582,788 seconds (828 days or ~2 years). At an iteration rate of 500 Hz, you can only run for 99 days before overflowing.

Now if that is a problem, you could do something like the following:
1
2
3
4
5
6
7
8
9
10
11
12
const unsigned int OverflowLimit = 4000000000;
clock_t RawTime = clock();
static unsigned int overflows = 0;

if (RawTime >= OverflowLimit )
{
  RawTime -= OverflowLimit ;
  overflows++;
}

// Now port this to a 64bit integer
long long TotalTime = overflows*OverflowLimit + RawTime;


EDIT: I was ninja'd by ausairman. I didn't take the sign bit into account, Change OverflowLimit to account for this.
Last edited on
Topic archived. No new replies allowed.