Concurrency issues in reader-writer problem

Discussion in 'C++' started by Hunk, Feb 21, 2007.

  1. Hunk

    Hunk Guest

    Hi

    I would like some ideas on way to solve the concurrency issue. The
    problem is , I have an object say X which multiple readers need to
    access and also to update. They do so by say two functions :
    // simplistic view of the problem
    Read()
    {
    return X;
    }

    Update(Y)
    {
    X= Y;
    }

    Now I would like to block any reader while calling update. With the
    general solutions provided of having say a mutex and using it in Read
    and Update would solve the problem. But then this would block multiple
    readers also which is not desired. i.e a block on read should be
    allowed only when update is called. Having a boolean and checkin is
    not ideal as the atomicity of the operation is not guaranteed. Any
    ideas on this?
    Hunk, Feb 21, 2007
    #1
    1. Advertising

  2. Hunk

    dasjotre Guest

    On 21 Feb, 11:08, "Hunk" <> wrote:
    > Hi
    >
    > I would like some ideas on way to solve the concurrency issue. The
    > problem is , I have an object say X which multiple readers need to
    > access and also to update. They do so by say two functions :
    > // simplistic view of the problem
    > Read()
    > {
    > return X;
    >
    > }
    >
    > Update(Y)
    > {
    > X= Y;
    >
    > }
    >
    > Now I would like to block any reader while calling update. With the
    > general solutions provided of having say a mutex and using it in Read
    > and Update would solve the problem. But then this would block multiple
    > readers also which is not desired. i.e a block on read should be
    > allowed only when update is called. Having a boolean and checkin is
    > not ideal as the atomicity of the operation is not guaranteed. Any
    > ideas on this?


    you nead reader-writer lock. the best implementation I've
    seen is Valery Pryamikov's mrsw_guard
    dasjotre, Feb 21, 2007
    #2
    1. Advertising

  3. Hunk

    Bharath Guest

    On Feb 21, 3:08 am, "Hunk" <> wrote:
    > Hi
    >
    > I would like some ideas on way to solve the concurrency issue. The
    > problem is , I have an object say X which multiple readers need to
    > access and also to update. They do so by say two functions :
    > // simplistic view of the problem
    > Read()
    > {
    > return X;
    >
    > }
    >
    > Update(Y)
    > {
    > X= Y;
    >
    > }
    >
    > Now I would like to block any reader while calling update. With the
    > general solutions provided of having say a mutex and using it in Read
    > and Update would solve the problem. But then this would block multiple
    > readers also which is not desired. i.e a block on read should be
    > allowed only when update is called. Having a boolean and checkin is
    > not ideal as the atomicity of the operation is not guaranteed. Any
    > ideas on this?


    Is this a multi-threaded program? If it is, the best solution is to
    use read-write locks in pthread library. See the manpage of
    pthread_rwlock_wrlock and pthread_rwlock_rdlock. Note that you need to
    have -DUSE_UNIX98 in your compiler flags.
    Bharath, Feb 21, 2007
    #3
  4. Hunk

    Hunk Guest

    On Feb 21, 7:55 pm, "Bharath" <> wrote:
    > On Feb 21, 3:08 am, "Hunk" <> wrote:
    >
    >
    >
    >
    >
    > > Hi

    >
    > > I would like some ideas on way to solve the concurrency issue. The
    > > problem is , I have an object say X which multiple readers need to
    > > access and also to update. They do so by say two functions :
    > > // simplistic view of the problem
    > > Read()
    > > {
    > > return X;

    >
    > > }

    >
    > > Update(Y)
    > > {
    > > X= Y;

    >
    > > }

    >
    > > Now I would like to block any reader while calling update. With the
    > > general solutions provided of having say a mutex and using it in Read
    > > and Update would solve the problem. But then this would block multiple
    > > readers also which is not desired. i.e a block on read should be
    > > allowed only when update is called. Having a boolean and checkin is
    > > not ideal as the atomicity of the operation is not guaranteed. Any
    > > ideas on this?

    >
    > Is this a multi-threaded program? If it is, the best solution is to
    > use read-write locks in pthread library. See the manpage of
    > pthread_rwlock_wrlock and pthread_rwlock_rdlock. Note that you need to
    > have -DUSE_UNIX98 in your compiler flags.- Hide quoted text -
    >
    > - Show quoted text -


    Thanks... yes it is a multithreaded program. i'll take a look at the
    reader writer locks mentioned. Any idea on how this is implemented in
    zthreads?
    Hunk, Feb 22, 2007
    #4
  5. Hunk

    Bharath Guest

    On Feb 21, 9:59 pm, "Hunk" <> wrote:
    > On Feb 21, 7:55 pm, "Bharath" <> wrote:
    >
    >
    >
    >
    >
    > > On Feb 21, 3:08 am, "Hunk" <> wrote:

    >
    > > > Hi

    >
    > > > I would like some ideas on way to solve the concurrency issue. The
    > > > problem is , I have an object say X which multiple readers need to
    > > > access and also to update. They do so by say two functions :
    > > > // simplistic view of the problem
    > > > Read()
    > > > {
    > > > return X;

    >
    > > > }

    >
    > > > Update(Y)
    > > > {
    > > > X= Y;

    >
    > > > }

    >
    > > > Now I would like to block any reader while calling update. With the
    > > > general solutions provided of having say a mutex and using it in Read
    > > > and Update would solve the problem. But then this would block multiple
    > > > readers also which is not desired. i.e a block on read should be
    > > > allowed only when update is called. Having a boolean and checkin is
    > > > not ideal as the atomicity of the operation is not guaranteed. Any
    > > > ideas on this?

    >
    > > Is this a multi-threaded program? If it is, the best solution is to
    > > use read-write locks in pthread library. See the manpage of
    > > pthread_rwlock_wrlock and pthread_rwlock_rdlock. Note that you need to
    > > have -DUSE_UNIX98 in your compiler flags.- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > Thanks... yes it is a multithreaded program. i'll take a look at the
    > reader writer locks mentioned. Any idea on how this is implemented in
    > zthreads?- Hide quoted text -
    >
    > - Show quoted text -


    I never used zthreads but this link might help.
    http://zthread.sourceforge.net/html/classZThread_1_1FairReadWriteLock.html
    Bharath, Feb 22, 2007
    #5
  6. "dasjotre" <> wrote in message
    news:...
    > On 21 Feb, 11:08, "Hunk" <> wrote:

    [...]
    > you nead reader-writer lock. the best implementation I've
    > seen is Valery Pryamikov's mrsw_guard


    how does it compare to the following algorithm:
    Chris Thomasson, Feb 23, 2007
    #6
  7. Chris Thomasson, Feb 23, 2007
    #7
  8. Chris Thomasson, Feb 23, 2007
    #8
  9. Chris Thomasson, Feb 23, 2007
    #9
  10. "Chris Thomasson" <> wrote in message
    news:p...
    > DOH! sorry for the other msg... I left out the link!
    >
    > "dasjotre" <> wrote in message
    > news:...
    >> On 21 Feb, 11:08, "Hunk" <> wrote:

    > [...]
    >> you nead reader-writer lock. the best implementation I've
    >> seen is Valery Pryamikov's mrsw_guard

    >
    > how does it compare to the following algorithm:
    >
    > http://groups.google.com/group/comp.programming.threads/browse_frm/thread/5ffb0ed46a4bcb4b


    Or this rw-spinlock implementation:

    http://appcore.home.comcast.net/appcore/src/ac_rwspinlock_algo1_c.html

    http://appcore.home.comcast.net/appcore/include/ac_rwspinlock_algo1_h.html
    Chris Thomasson, Feb 23, 2007
    #10
  11. Hunk

    Hunk Guest

    On Feb 23, 11:39 am, "Chris Thomasson" <> wrote:
    > A while back I was thinking about fully integrating a working solution to
    > the reader-writer problem directly into the C++ STL:
    >
    > http://groups.google.com/group/comp.lang.c .moderated/browse_frm/thr...


    Hey chris , thanks a lot for the ton of information given. I'm just
    browsing through it.. appears to be good. i'll just see if there's a
    way to choose between the implementations.
    Hunk, Feb 23, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Spliff Monkey

    Reader Writer monitor in JAVA

    Spliff Monkey, Mar 29, 2005, in forum: Java
    Replies:
    4
    Views:
    11,112
    A. Bolmarcich
    Mar 30, 2005
  2. Roger Flores

    ANNOUNCE: new XML reader and writer

    Roger Flores, Feb 13, 2004, in forum: XML
    Replies:
    0
    Views:
    442
    Roger Flores
    Feb 13, 2004
  3. Matthew Scott
    Replies:
    0
    Views:
    302
    Matthew Scott
    May 6, 2005
  4. DaVinci
    Replies:
    1
    Views:
    563
    Piotr Kobzda
    Oct 5, 2006
  5. Henri
    Replies:
    1
    Views:
    374
    Henri
    Jul 13, 2004
Loading...

Share This Page