读写器问题中的并发问题 [英] Concurrency issues in reader-writer problem

查看:80
本文介绍了读写器问题中的并发问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想解决并发问题的一些想法。

问题是,我有一个对象说X,多个读者需要访问
并且还要更新。他们这样说是两个功能:

//简单的问题视图

阅读()

{

返回X;

}


更新(Y)

{

X = Y;

}


现在我想在调用update时阻止任何读者。使用

一般解决方案提供了一个互斥量并在Read

和Update中使用它可以解决问题。但是这也会阻止多个

读者,这也是不可取的。即只有在调用更新时才允许读取块。

。有一个布尔和签入是
不理想,因为无法保证操作的原子性。任何

想法吗?

解决方案

2月21日,11:08,Hunk < santosh.udyav ... @ gmail.comwrote:





我想要一些想法解决并发问题。

问题是,我有一个对象说X,多个读者需要访问
并且还要更新。他们这样说是两个功能:

//简单的问题视图

阅读()

{

返回X;


}


更新(Y)

{

X = Y;


}


现在我想在调用update时阻止任何读者。使用

一般解决方案提供了一个互斥量并在Read

和Update中使用它可以解决问题。但是这也会阻止多个

读者,这也是不可取的。即只有在调用更新时才允许读取块。

。有一个布尔和签入是
不理想,因为无法保证操作的原子性。对此有什么想法?b $ b想法?



你是读者 - 作家锁定的。最好的实施我已经看到了
是Valery Pryamikov的mrsw_guard


2月21日凌晨3:08 ,Hunk < santosh.udyav ... @ gmail.comwrote:





我想要一些想法解决并发问题。

问题是,我有一个对象说X,多个读者需要访问
并且还要更新。他们这样说是两个功能:

//简单的问题视图

阅读()

{

返回X;


}


更新(Y)

{

X = Y;


}


现在我想在调用update时阻止任何读者。使用

一般解决方案提供了一个互斥量并在Read

和Update中使用它可以解决问题。但是这也会阻止多个

读者,这也是不可取的。即只有在调用更新时才允许读取块。

。有一个布尔和签入是
不理想,因为无法保证操作的原子性。对此有什么想法?b $ b想法?



这是一个多线程程序吗?如果是,最好的解决方案是使用pthread库中的读写锁。
。请参阅

pthread_rwlock_wrlock和pthread_rwlock_rdlock的联机帮助页。请注意,您需要在编译器标志中使用-BUSE_UNIX98




2月21日晚上7:55, ;巴拉斯" < cbku ... @ gmail.comwrote:


2月21日凌晨3:08,Hunk < santosh.udyav ... @ gmail.comwrote:


Hi


我想在解决并发问题的方法上有一些想法。

问题是,我有一个对象说X,多个读者需要访问
并且还要更新。他们这样说是两个功能:

//简单的问题视图

阅读()

{

返回X;


}


更新(Y)

{

X = Y;


}


现在我想阻止任何读者调用更新。使用

一般解决方案提供了一个互斥量并在Read

和Update中使用它可以解决问题。但是这也会阻止多个

读者,这也是不可取的。即只有在调用更新时才允许读取块。

。有一个布尔和签入是
不理想,因为无法保证操作的原子性。对此有什么想法?b $ b想法?



这是一个多线程程序吗?如果是,最好的解决方案是使用pthread库中的读写锁。
。请参阅

pthread_rwlock_wrlock和pthread_rwlock_rdlock的联机帮助页。请注意,您需要在编译器标志中使用-BUSE_UNIX98.-隐藏引用文本 -


- 显示引用文本 -



谢谢......是的,这是一个多线程程序。我会看看提到的

读者作家锁。关于如何在

zthreads中实现这一点的任何想法?


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?

解决方案

On 21 Feb, 11:08, "Hunk" <santosh.udyav...@gmail.comwrote:

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


On Feb 21, 3:08 am, "Hunk" <santosh.udyav...@gmail.comwrote:

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.


On Feb 21, 7:55 pm, "Bharath" <cbku...@gmail.comwrote:

On Feb 21, 3:08 am, "Hunk" <santosh.udyav...@gmail.comwrote:


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?


这篇关于读写器问题中的并发问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆