简体   繁体   English

如何在windows中用c / c ++实现hello world共享内存?

[英]How to implement a hello world shared memory in c/c++ in windows?

Is shared memory stable at the first place? 共享内存是否稳定在第一位?

I prefer this way to inter-process/application communication because that way I don't need the overhead of parsing data. 我更喜欢这种方式来进行进程间/应用程序通信,因为这样我不需要解析数据的开销。

Is there a good hello world demo on this in c/c++? 在c / c ++中是否有一个很好的hello world演示?

You should check out Boost.Interprocess . 你应该看看Boost.Interprocess It provides the functionality you need and the documentation contains instructions on how to use it. 它提供了您需要的功能,文档包含有关如何使用它的说明。

Is shared memory stable at the first place? 共享内存是否稳定在第一位?

Yes. 是。

I prefer this way to inter-process/application communication because that way I don't need the overhead of parsing data. 我更喜欢这种方式来进行进程间/应用程序通信,因为这样我不需要解析数据的开销。

You may be wrong. 你可能错了。

Is there a good hello world demo on this in c/c++? 在c / c ++中是否有一个很好的hello world演示?

I that you want something like Managing Memory-Mapped Files . 我想要你喜欢管理内存映射文件

There are some issues you need to think of when using shared memory: 使用共享内存时需要考虑一些问题:

  1. You need to lock accesses to the shared memory so no process attempts to read from it while another process is writing to it to prevent reading a partially updated (inconstant). 您需要锁定对共享内存的访问,以便在另一个进程写入它时,没有进程尝试从中读取,以防止读取部分更新(不稳定)。
  2. You need some way to handle corruption of the shared memory. 您需要一些方法来处理共享内存的损坏。 What happens when a program that is writing to the shared memory crashes and leaves the shared memory in corrupt? 当写入共享内存的程序崩溃并使共享内存损坏时会发生什么?

It's as stable as your code. 它和你的代码一样稳定。

Note that there's no parsing required with any IPC method that supports binary messages,, such as UDP datagrams, message-mode pipes, or mailslots. 请注意,任何支持二进制消息的IPC方法都不需要解析,例如UDP数据报,消息模式管道或邮件槽。

如果您正在寻找一个简单的示例, MSDN 的动态链接库中使用共享内存可能是一个很好的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM