是否有“upgrade_to_unique_lock”?对于 boost::进程间?

发布于 2024-10-01 10:55:02 字数 985 浏览 3 评论 0原文

我正在寻找在偏向写入器读取器/写入器模型中在两个(或更多)进程之间有效共享数据块的最佳方法。

我当前的测试是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你好,陌生人 2024-10-08 10:55:02

所有 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.

浊酒尽余欢 2024-10-08 10:55:02

OP 的解决方案。

正如问题注释中所述,答案是使用成员函数 unlock_upgradable_and_lock。如果有一个与 upgrade_to_unique_lock 类似的 boost::interprocess ,我不知道它在哪里。但是 writer() 函数可以重写为:

while (1) {
  // Get upgrade lock on the mutex
  myMutex.lock_upgradable();

  // Get exclusive access and block everyone else
  myMutex.unlock_upgradable_and_lock();

  // Write values here

  // Unlock the mutex (and stop blocking readers)
  myMutex.unlock();
}

Solution by OP.

The answer, as stated in the question comments is to use the member function unlock_upgradable_and_lock. If there is an boost::interprocess analog to upgrade_to_unique_lock, I don't know where it is. But the writer() function can be rewritten as:

while (1) {
  // Get upgrade lock on the mutex
  myMutex.lock_upgradable();

  // Get exclusive access and block everyone else
  myMutex.unlock_upgradable_and_lock();

  // Write values here

  // Unlock the mutex (and stop blocking readers)
  myMutex.unlock();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文