pthread_rwlock_lock vs pthread_mutex_lock

I have been wondering what the difference between pthread_rwlock_lock and pthread_mutex_lock is. Both these routines acquire an exclusive rw lock on an enclosed region.

So I performed a simple experiment in which I execute both these routines multiple times in a loop. Here are the results:
Time taken for 8388608 loops of rwlock = 1.077198 sec
Time taken for 8388608 loops of mutex_lock = 0.337276 sec

As you can see, mutex locking is much faster than the rwlock_lock. Then when would one prefer to use pthread_rwlock_lock over a mutex lock ??

Thanks for your help.

This is not surprising at all. An rwlock can also be "rdlock"ed, while a mutex can't. This means an rwlock can be in one of the three states:

  1. free
  2. locked with one wrlock
  3. locked with one or more rdlocks

if you don't need the capability to have multiple readers at the same time, you should normally prefer mutexes.