1 /* a small program to benchmark locking primitives with different implementations */ 2 3 #include <pthread.h> 4 #include <sys/time.h> 5 #include <stdio.h> 6 7 static double now(void) 8 { 9 struct timeval tv; 10 gettimeofday(&tv, NULL); 11 return tv.tv_sec + tv.tv_usec/1000000.0; 12 } 13 14 int main( void ) 15 { 16 double t0, t1; 17 pthread_mutex_t lock1 = PTHREAD_MUTEX_INITIALIZER; 18 int volatile lock2 = 0; 19 long count; 20 const long ITERATIONS = 1000000; 21 22 /* pthread_mutex_lock */ 23 t0 = now(); 24 for (count = ITERATIONS; count > 0; count--) { 25 pthread_mutex_lock(&lock1); 26 pthread_mutex_unlock(&lock1); 27 } 28 t1 = now() - t0; 29 printf( "pthread_mutex_lock/unlock: %.5g us/op\n", (t1*1000000.0)/ITERATIONS ); 30 31 return 0; 32 } 33