Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #355

Re: Mutex or reader writer lock

From Edek <edek.pienkowski@gmail.com>
Newsgroups comp.programming.threads
Subject Re: Mutex or reader writer lock
Date 2011-06-23 00:14 +0200
Organization ATMAN - ATM S.A.
Message-ID <ittper$joe$1@node2.news.atman.pl> (permalink)
References <b10602ad-9b3b-485e-bb1f-66b0e1b3adce@h38g2000pro.googlegroups.com>

Show all headers | View raw


On 06/21/2011 12:27 PM, ittium wrote:
> Hi group,
> I plan to use pthread read write lock for one of my products. I did
> some research and found out that reader writer lock consume more CPU
> than mutex. Folloiwng is my understanding, please comment on these
> points

>
> 1. There are mostly read operations on the shared data and there are
> **large** number of readers so even if there is cpu penalty of using
> reader writer lock, multiple threads can simultaneously work with
> rwlock

That's the basic idea of an rwlock. I assume this post is not related
to your nearby posts, where readers just fetch something under lock
and work on it later outside.

.
>
> 2. If  there are mostly read operations but there are **not many
> readers** than contention will be small and it is better to use mutex
> since mutex locking/unlocking is light weight

"Not many readers" is not enough information. a) comment above,
b) contention depends not only on count, but also on time of operations
and frequency, sometimes even patterns

>
> 3.If read operations are **heavy weight**, **irrespective of number of
> readers**, It is always a good idea to use reader writer locks in
> place of mutex since so even if there is cpu penalty of using reader
> writer lock, multiple threads can simultaneously work
>

See above. In general the topic of lock granularity comes to (my) mind,
a way to lower the contention. I mean, if there is a really
"heavy weight" operation holding a lock all the time, something has to
wait. Even under RWLock, you can imagine one reader thread blocking
everything else for however long it runs.

And,
> **irrespective of number of
>> readers**

if there is one, there is no need for an rwlock, FWIW.


> I will also appreciate, few lines on pthread reader writer locks
> implementation. Is is using futex and condition variables ?
>

I don't know what platform you are referring to, but atomic + futex
as far as I know (plus bookkeeping of course)


> thanks
> Ittium

Edek

Back to comp.programming.threads | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Mutex or reader writer lock ittium <ittium@gmail.com> - 2011-06-21 03:27 -0700
  Re: Mutex or reader writer lock David Schwartz <davids@webmaster.com> - 2011-06-21 22:03 -0700
  Re: Mutex or reader writer lock Edek <edek.pienkowski@gmail.com> - 2011-06-23 00:14 +0200
    Re: Mutex or reader writer lock ittium <ittium@gmail.com> - 2011-06-23 06:04 -0700
    Re: Mutex or reader writer lock "Chris M. Thomasson" <cristom@charter.net> - 2011-06-23 11:43 -0700
      Re: Mutex or reader writer lock ittium <ittium@gmail.com> - 2011-06-24 02:26 -0700
        Re: Mutex or reader writer lock Edek <edek.pienkowski@gmail.com> - 2011-06-24 12:02 +0200
          Re: Mutex or reader writer lock "Chris M. Thomasson" <cristom@charter.net> - 2011-06-29 13:46 -0700
            Re: Mutex or reader writer lock "Chris M. Thomasson" <cristom@charter.net> - 2011-06-29 14:26 -0700
              Re: Mutex or reader writer lock "Chris M. Thomasson" <cristom@charter.net> - 2011-06-29 20:30 -0700

csiph-web