views:

75

answers:

1

I have a code that "sounds" like this:

thread 1

now = rdtsc();
for_each_member_in_a_list {
   if ( member_in_list.deadline() <= now ) {
      do_something;
   }
}

thread 2

now = rdtsc();
for_each_member_in_a_list {
   member_in_list.updatedealine( foo(now, ...) );
}

now while this was working good in the past now with a SMP system this code doesn't work as intended. I think I will use clock_gettime with CLOCK_MONOTONIC, but I would like to ear some other hints first.

+1  A: 

In theory TSC should be the syncronised between all the CPU's on a motherboard, but in some SMP systems it's not, blame the motherboard manufacturer. Also, on some older chips TSC seems to change with the power state of the CPU making it potentially very unreliable. clock_gettime(CLOCK_MONOTONIC) is more reliable, but has more overhead (it's a systemcall), but is by far the best way to do this.

I also assume you're locking your data structures :)

Perry Lorier
I'm using lockless datastructures.
Gaetano Mendola