[opensuse-programming] pthread_rwlock_wrlock and pthread_mutex_lock?
What's the difference between pthread_rwlock_wrlock and pthread_mutex_lock? I understand the difference if we take pthread_rwlock_rdlock into account, but is there any difference between using purely pthread_rwlock_wrlock() vs. pthread_mutex_lock() ? /Per Jessen, Zürich -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
On 02/11/2010 04:35 AM, Per Jessen wrote:
What's the difference between pthread_rwlock_wrlock and pthread_mutex_lock? I understand the difference if we take pthread_rwlock_rdlock into account, but is there any difference between using purely pthread_rwlock_wrlock() vs. pthread_mutex_lock() ?
Let me try to answer this since I've implemented this type of thing
before. The pthread_mutex_lock is a general purpose lock object. The
locking thread causes all other threads that use that mutex to block.
Very simple. The pthread_rwlock_wrlock() and its sibling
pthread_rwlock_rdlock() implement a reader-writer strategy there are
many readers and a few writers. The basic issue is that you want to
allow reader threads to proceed, but you want to block writers. If a
writer thread calls pthread_rwlock_wrlock(), it will block if a reader
thread has called the rdlock, but it will also block subsequent readers.
So basically, in your question, if you only use wrlock() then the effect
is the same as the standard mutex lock, but it appears there is more
overhead associated with it.
--
Jerry Feldman
Jerry Feldman wrote:
On 02/11/2010 04:35 AM, Per Jessen wrote:
What's the difference between pthread_rwlock_wrlock and pthread_mutex_lock? I understand the difference if we take pthread_rwlock_rdlock into account, but is there any difference between using purely pthread_rwlock_wrlock() vs. pthread_mutex_lock() ?
Let me try to answer this since I've implemented this type of thing before. The pthread_mutex_lock is a general purpose lock object. The locking thread causes all other threads that use that mutex to block. Very simple. The pthread_rwlock_wrlock() and its sibling pthread_rwlock_rdlock() implement a reader-writer strategy there are many readers and a few writers. The basic issue is that you want to allow reader threads to proceed, but you want to block writers. If a writer thread calls pthread_rwlock_wrlock(), it will block if a reader thread has called the rdlock, but it will also block subsequent readers. So basically, in your question, if you only use wrlock() then the effect is the same as the standard mutex lock, but it appears there is more overhead associated with it.
Thanks, that is what I thought too, but I was getting results inconsistent with that. I was just wondering if my understanding was flawed :-) /Per Jessen, Zürich -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
On 02/13/2010 11:26 AM, Per Jessen wrote:
Jerry Feldman wrote:
On 02/11/2010 04:35 AM, Per Jessen wrote:
What's the difference between pthread_rwlock_wrlock and pthread_mutex_lock? I understand the difference if we take pthread_rwlock_rdlock into account, but is there any difference between using purely pthread_rwlock_wrlock() vs. pthread_mutex_lock() ?
Let me try to answer this since I've implemented this type of thing before. The pthread_mutex_lock is a general purpose lock object. The locking thread causes all other threads that use that mutex to block. Very simple. The pthread_rwlock_wrlock() and its sibling pthread_rwlock_rdlock() implement a reader-writer strategy there are many readers and a few writers. The basic issue is that you want to allow reader threads to proceed, but you want to block writers. If a writer thread calls pthread_rwlock_wrlock(), it will block if a reader thread has called the rdlock, but it will also block subsequent readers. So basically, in your question, if you only use wrlock() then the effect is the same as the standard mutex lock, but it appears there is more overhead associated with it.
Thanks, that is what I thought too, but I was getting results inconsistent with that. I was just wondering if my understanding was flawed :-)
One advantage of these lock strategies over a single mutex lock is that
there will be no starvation. A writer thread gets a certain priority. If
you try to implement read/write locks in the standard mutex, you can
cause writers to be permanently locked out if you have enough reader
traffic. I can tell you from experience that it is not easy to design a
decent reader/writer lock scheme.
--
Jerry Feldman
participants (2)
-
Jerry Feldman
-
Per Jessen