是否有“upgrade_to_unique_lock”?对于 boost::进程间?
我正在寻找在偏向写入器读取器/写入器模型中在两个(或更多)进程之间有效共享数据块的最佳方法。
我当前的测试是使用 boost::interprocess
进行的。我创建了一些托管共享内存
,并尝试使用存储在共享内存中的进程间互斥锁来锁定对数据块的访问。
但是,即使在读取器上使用 sharable_lock
并在写入器上使用 upgradable_lock
,客户端也会在写入操作期间读取碎片值,而不是阻塞。在单个进程中的线程之间进行类似的读取器/写入器设置时,我使用 upgrade_to_unique_lock
来解决此问题。但是,我还没有找到其等效的 boost::interprocess
。有吗?
服务器(写入器):
while (1) {
// Get upgrade lock on the mutex
upgradable_lock <MutexType> lock(myMutex);
// Need 'upgrade_to_unique_lock' here so shared readers will block until
// write operation is finished.
// Write values here
}
客户端(读取器)
while (1)
{
// Get shared access
sharable_lock <MutexType> lock(myMutex);
// Read p1's data here -- occasionally invalid!
}
我想手头更大的问题是:进程间互斥体是否是在写入器偏向设置中访问进程之间共享内存的正确方法?
注意:使用 Boost 1.44.0
I am looking for the best way to effectively share chunks of data between two (or more) processes in a writer-biased reader/writer model.
My current tests are with boost::interprocess
. I have created some managed_shared_memory
and am attempting to lock access to the data chunk by using an interprocess mutex stored in the shared memory.
However, even when using sharable_lock
on the reader and upgradable_lock
on the writer, the client will read fragmented values during write operations instead of blocking. While doing a similar reader/writer setup between threads in a single process, I used upgrade_to_unique_lock
to solve this issue. However, I have not found its boost::interprocess
equivalent. Does one exist?
Server (writer):
while (1) {
// Get upgrade lock on the mutex
upgradable_lock <MutexType> lock(myMutex);
// Need 'upgrade_to_unique_lock' here so shared readers will block until
// write operation is finished.
// Write values here
}
Client (reader)
while (1)
{
// Get shared access
sharable_lock <MutexType> lock(myMutex);
// Read p1's data here -- occasionally invalid!
}
I guess the bigger question at hand is this: is an interprocess mutex even the proper way to access shared memory between processes in a writer-biased setup?
Note: using Boost 1.44.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有 Boost.Interprocess 可升级锁均支持升级 这个。定义此处。
关于你更广泛的问题 - 我认为这正是你想要的。读者仍然可以并发工作,并且您必须防止并发写入。除非您可以对共享内存进行分区以保证更受限制的访问,否则这看起来是最好的。
All Boost.Interprocess upgradable locks support upgrade per this. Definition here.
Regarding your broader question - I would think that this is precisely what you want. Readers can still work concurrently, and you have to prevent concurrent writes. Unless you can partition the shared memory such that more constrained access is guaranteed, this looks the best.
OP 的解决方案。
正如问题注释中所述,答案是使用成员函数
unlock_upgradable_and_lock
。如果有一个与upgrade_to_unique_lock
类似的boost::interprocess
,我不知道它在哪里。但是 writer() 函数可以重写为:Solution by OP.
The answer, as stated in the question comments is to use the member function
unlock_upgradable_and_lock
. If there is anboost::interprocess
analog toupgrade_to_unique_lock
, I don't know where it is. But thewriter()
function can be rewritten as: