pthread_rwlock_lock vs pthread_mutex_lock


 
Thread Tools Search this Thread
Top Forums Programming pthread_rwlock_lock vs pthread_mutex_lock
# 1  
Old 12-06-2008
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.
# 2  
Old 12-08-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Programming

pthread_mutex_lock in ANSI C vs using Atomic builtins of GCC

I have a program which has 7-8 threads, and lots of shared variables; these variables (and also they may not the primitive type, they may be enum or struct ), then they may read/write by different threads at the same time. Now, my design is like this, typedef unsigned short int UINT16;... (14 Replies)
Discussion started by: sehang
14 Replies

2. Programming

Interesting issue with pthread_mutex_lock and siglongjmp in AIX 5.3 (and no other OS)

Executive summary: Code (posted below) cores in AIX 5.3, despite being compiled and run successfully on several other operating systems. Code is attempting to verify that pthread_mutex_lock can be successfully aborted by siglongjmp. I do not believe this is an unreasonable requirement. If... (1 Reply)
Discussion started by: DreamWarrior
1 Replies
Login or Register to Ask a Question