[英]Writing to boost shared memory
It must have been straight forward answer, but I haven't found anywhere how to do it... 它一定是直截了当的答案,但我没有找到任何地方怎么做...
I have successfully created a shared memory segment using boost IPC system as in the example: 我已经使用boost IPC系统成功创建了一个共享内存段,如下例所示:
boost::interprocess::managed_shared_memory segment(boost::interprocess::create_only, "MySharedMemory", 65536);
sharedData = segment.construct<MyType>("Test")(0, 0.2);
I have also been able to read the values from a different process. 我也能够从不同的过程中读取值。 What I cannot understand is how to edit the values of this variable (If I'm allowed to call "Test" as a variable) and read them from the other process.
我无法理解的是如何编辑此变量的值(如果我允许将“Test”称为变量)并从其他进程读取它们。 I want to be on a loop and write these values.
我想循环并写下这些值。
Thank you 谢谢
This isn't a good idea because there is no way of enforcing concurrency on a shared memory block. 这不是一个好主意,因为没有办法在共享内存块上强制执行并发。 In the same way a shared resource needs to be protected from multiple threads crashing into each other (eg with a mutex or critical section) the same is true for a shared memory block.
以同样的方式,需要保护共享资源免受多个线程相互冲突(例如,使用互斥或临界区),对于共享内存块也是如此。
Without an extra signalling mechanism using something like named pipes, there is no way of safely signalling that a shared memory block is 如果没有使用命名管道之类的额外信令机制,则无法安全地发信号通知共享内存块
If you create your memory block with the read_write
flag it will set the correct Windows permissions. 如果使用
read_write
标志创建内存块,则会设置正确的Windows权限。 The example in the boost documentation shows this. boost文档中的示例显示了这一点。
using boost::interprocess;
shared_memory_object shm_obj
(open_only //only open
,"shared_memory" //name
,read_write //read-write mode
);
As @Konrad suggested, using shared memory so loosely is not A Good Thing™. 正如@Konrad建议的那样,使用共享内存如此松散并不是一件好事。 That being said, Boost does provide interprocess synchronization utilities that work much the same as those traditionally used between threads.
话虽这么说,Boost确实提供了进程间同步实用程序,它们与传统上在线程之间使用的实用程序大致相同。
Give this page of the documentation a good read (particularly the section on Conditions) and see if that might give you an idea of what should be aiming for. 给这个文档的页面一个很好的阅读(特别是条件部分),看看是否可以让你知道应该瞄准什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.