At least on Linux systems (Fedora/RH/CentOS), the gettimeofday call can bite you when used as a base time for calls into the pthread_cond_timedwait call. Case in point, the "powers that be" adjusted the world time backwards by one second the weekend of July 1, 2012. All of a sudden a call to Semaphore::tryWait(1000) was returning false (not set) immediately causing the process to chew up a bunch of useless cpu cycles. This was due to the Semaphore.cpp implementation of waitImpl which calls gettimeofday to a acquire a base time. The 1000 milliseconds was then added to the struct timeval. Then a call was made to pthread_cond_timedwait.
However, pthread_cond_timewait (at least on Linux) is running off of CLOCK_REALTIME which is available from clock_gettime. This caused the net time of gettimeofday + 1000 to (roughly) equal clock_gettime(CLOCK_REALTIME) and the wait timer immediately expired.
I know not all systems have clock_gettime (MacOS for instance), but perhaps there is a better choice than gettimeofday. There is also a way to specify a clock of choice on the condition variable, but that may be even less portable as that's a later addition to pthreads.
For us, we'll simply swap clock_gettime(CLOCK_REALTIME...) which is the default clock for the condition variable.





