问题描述
我正在寻找一种以 writer-biased 读取器/写入器模型在两个(或多个)进程之间有效共享数据块的最佳方法.
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.
我当前的测试是使用boost::interprocess
.我已经创建了一些managed_shared_memory
,并且正在尝试使用存储在共享内存中的进程间互斥锁来锁定对数据块的访问.
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.
但是,即使在读取器上使用sharable_lock
并在写入器上使用upgradable_lock
时,客户端也会在写操作期间读取碎片值,而不是阻塞.在单个进程中在线程之间进行类似的读取器/写入器设置时,我使用了upgrade_to_unique_lock
来解决此问题.但是,我还没有找到它的boost::interprocess
等效项.是否存在?
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?
服务器(写程序):
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!
}
我想现在面临的一个更大的问题是:进程间互斥体甚至是在写者偏爱的设置中访问进程之间共享内存的正确方法吗?
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?
注意:使用Boost 1.44.0
推荐答案
所有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.
这篇关于是否存在用于boost :: interprocess的"upgrade_to_unique_lock"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!