Concurrency issues in reader-writer problem

H

Hunk

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?
 
D

dasjotre

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
 
B

Bharath

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.
 
H

Hunk

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?
 
C

Chris Thomasson

dasjotre said:
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:
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top