clock() time travel.

Paweł Sikora pluto@agmk.net
Thu Jan 16 10:54:00 GMT 2014


Hi,

i've observed on my i3-540 cpu that subsequent clock() calls 
*sometimes* give smaller number of ticks than previous one.
is it a known issue?

BR,
Paweł.

% ./timing
t[current]: 10713902 < t[previous]: 10713903
zsh: abort (core dumped)  ./timing

gcc-4.8.2-7.fc20.x86_64
glibc-2.18-11.fc20.x86_64
kernel-3.12.7-300.fc20.x86_64

% cat timing.c
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <time.h>

void* th0( void* args )
{
	int N = 10000000, i;
	for ( i = 0; i < N; i++ )
	{
		sched_yield();
	}
	return NULL;
}

void* th1( void* args )
{
	int N = 10000000, i;
	for ( i = 0; i < N; i++ )
	{
		clock_t t0 = clock();
		sched_yield();
		clock_t t1 = clock();
		if ( t1 < t0 )
		{
			printf( "t[current]: %ld < t[previous]: %ld\n", t1, t0 );
			__builtin_abort();
		}
	}
	return NULL;
}

int main( int argc, char** argv )
{
	pthread_t t0, t1;
	pthread_create( &t0, NULL, th0, NULL );
	pthread_create( &t1, NULL, th1, NULL );
	pthread_join( t0, NULL );
	pthread_join( t1, NULL );
	return 0;
}



More information about the Libc-help mailing list