POSIX跨进程的共享内存同步C ++ / C ++ 11 [英] POSIX Shared Memory Sync Across Processes C++/C++11

查看:259
本文介绍了POSIX跨进程的共享内存同步C ++ / C ++ 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题(简​​而言之):
我使用POSIX共享内存,目前只使用POSIX信号量,我需要控制多个读者,多个作者。我需要帮助我使用什么变量/方法来控制访问在下面描述的限制。
我找到了一个我想实现的方法,但我不确定在使用POSIX共享内存时可以使用什么方法来实现它。

Problem (in short): I'm using POSIX Shared Memory and currently just used POSIX semaphores and i need to control multiple readers, multiple writers. I need help with what variables/methods i can use to control access within the limitations described below. I've found an approach that I want to implement but i'm unsure of what methodology i can use to implement it when using POSIX Shared memory.

找到的内容
http://stackoverflow.com/a/28140784
这个链接有我想使用的算法,但我不确定如何实现它与共享内存。我把类存储在共享内存中吗?这是我需要帮助的地方。
我不确定的原因是我的很多研究,指向保持共享内存到基元只是为了避免解决问题,STL对象不能使用。

What I've Found http://stackoverflow.com/a/28140784 This link has the algorithm i'd like to use but i'm unsure how to implement it with shared memory. Do i store the class in shared memory somehow? This is where I need help please. The reason I'm unsure is a lot of my research, points towards keeping shared memory to primitives only to avoid addressing problems and STL objects can't be used.

注意:
对于我的所有多线程,我使用C ++ 11功能。这个共享内存将是使用C ++ 11 std :: threads的完全独立的程序可执行文件,任何进程/可执行文件的任何线程都将从中访问。我已经避免了Linux pthread的任何我的多线程,并将继续这样做(除非它的控制变量不是实际的pThreads)。

NOTE: For all my multi-threading i'm using C++11 features. This shared memory will be completely seperate program executables using C++11 std::threads from which any thread of any process/executable will want access. I have avoided the Linux pthread for any of my multi-threading and will continue to do so (except if its just control variable not actual pThreads).

解决方案的参数


  • 必须可以在2+个进程之间共享,这将运行多个C ++ 11 std :: thread可能希望访问。也就是说多个写入器(一次排除一个),同时允许多个同时读取器,无需作者访问。

  • 不使用BOOST库。理想情况下,本机C ++ 11或内置的linux库,这将工作,而不需要安装抽象库。

  • 不使用pThread实际线程,但可以使用一些对象, with C ++ 11 std :: thread。

  • 理想情况下可以在运行时处理进程崩溃。例如。使用POSIX信号量,如果一个进程崩溃,当它有信号量,每个人都被拧。

  • Must be shareable between 2+ processes which will be running multiple C++11 std::thread that may wish access. I.e. Multiple Writers (exclusive one at a time) while allowing multiple simultaneous readers when no writer wants access.
  • Not using BOOST libraries. Ideally native C++11 or built in linux libraries, something that will work without the need to install abstract libraries.
  • Not using pThread actual threads but could use some object from there that will work with C++11 std::thread.
  • Ideally can handle a process crash while in operation. E.g. Using POSIX semaphore if a process crashes while it has the semaphore, everyone is screwed. I have seen people using file locks?

提前感谢

推荐答案


将共享内存保存到基元,以避免解决问题

keeping shared memory to primitives only to avoid addressing problems

可以在程序中使用指针和共享内存对象,只要内存 mmap 编译到同一个地址。这实际上是一个直截了当的提议,特别是在64位。请参阅我为实施详细信息写的开源C库: rszshm - 可调整大小的指针安全共享内存

You can use pointers in and to shared memory objects across programs, so long as the memory is mmaped to the same address. This is actually a straightforward proposition, especially on 64 bit. See this open source C library I wrote for implementation details: rszshm - resizable pointer-safe shared memory.


使用POSIX信号量,如果一个进程崩溃,当它有信号量,每个人都被拧。

Using POSIX semaphore if a process crashes while it has the semaphore, everyone is screwed.

如果你想使用OS中断的信号量,SysV信号量有SEM_UNDO,在这种情况下恢复。 OTOH pthread提供了强大的互斥体,可以在共享内存中嵌入和共享。这可以用于构建更复杂的机制。

If you want to use OS mediated semaphores, the SysV semaphores have SEM_UNDO, which recovers in this case. OTOH pthread offers robust mutexes that can be embedded and shared in shared memory. This can be used to build more sophisticated mechanisms.

在信号集中提供多个信号量的SysV方案,其中一组操作必须全部成功,或者调用块,允许建立复杂的机制。可以用一组三个信号量进行读/写锁定。

The SysV scheme of providing multiple semaphores in a semaphore set, where a group of actions must all succeed, or the call blocks, permits building sophisticated mechanism too. A read/write lock can be made with a set of three semaphores.

这篇关于POSIX跨进程的共享内存同步C ++ / C ++ 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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