views:

114

answers:

5

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++?

+2  A: 

You should check out Boost.Interprocess. It provides the functionality you need and the documentation contains instructions on how to use it.

radman
Is it true that I need to wrap a whole lot of libraries to use Boost?
wamp
Absolutely not true. Boost.Interprocess is a header only library, all you need to do is get the header files and include them in your project.
radman
Can you provide a demo how to use it?
wamp
I think your best way forward is to check out the quick start guide here: http://live.boost.org/doc/libs/1_43_0/doc/html/interprocess/quick_guide.html. It's as close as you'll get to a 'hello world' example.
radman
How can I lock the shared memory when writing to it,and release the lock when done?
wamp
A: 

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.

Ben Voigt
How can it be true that no parsing is required?
wamp
This of course assumes that both ends of the pipe are compiled by exactly the same build of the same compiler. Besides that, all bets are off, because the ABI is not standard.
Merlyn Morgan-Graham
@Merlyn: Depends on what data types you're using exactly. With POD types having the same pragma pack setting is enough. Of course if you are using any STL classes, etc., then the same library implementation needs to be used on both ends. But all these restrictions apply to shared memory as well.
Ben Voigt
@wamp: same way parsing isn't necessary in shared memory -- you cast your buffer pointer to some complex type and use it directly
Ben Voigt
@Ben Voigt: Agreed, PODs with compatible packing are fairly safe. If you don't control/compile all the code, and the classes use any number of non-POD features, that's when you're most likely to run into problems.
Merlyn Morgan-Graham
A: 

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++?

I that you want something like Managing Memory-Mapped Files.

ChrisW
No, I'm not communicating via a File(Managing Memory-Mapped Files)...
wamp
I don't understand your comment.
ChrisW
You are recommending communicating by writing to files,that's inefficient.
wamp
In Windows shared memory is implemented as a memory mapped file that has no file behind it, so that article is relevant, but I do understand your confusion
shf301
No, I think it's the way to do it, and that that's what it's called in the Win32 API. Yes you are writing to files; but more specifically you're writing to (shared) memory, which is backed by a file. Remember that a file isn't just on disk: the file system keeps a cache of the file in RAM (using disk as a backup e.g. if there's insufficient RAM).
ChrisW
How is that compared with Boost.Interprocess ?
wamp
@wamp I haven't used boost but I think you may find they're the same: that Boost uses/wraps the Win32 memory=mapped-file API. See [Native windows shared memory](http://www.boost.org/doc/libs/1_44_0/doc/html/interprocess/sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.windows_shared_memory), [Class windows_shared_memory](http://www.boost.org/doc/libs/1_44_0/doc/html/boost/interprocess/windows_shared_memory.html), and [boost/interprocess/windows_shared_memory.hpp](http://www.boost.org/doc/libs/1_40_0/boost/interprocess/windows_shared_memory.hpp).
ChrisW
A: 

If you're looking for a simple example, Using Shared Memory in a Dynamic-Link Library from MSDN is probably a good starting point.

bobbymcr
So shared memory can only be used in a DLL?
wamp
No you can use it in an EXE, the example just shows it implemented in a DLL. The idea there being that each program that's wants to communicate references the same DLL.
shf301
Is it efficient?
wamp
+1  A: 

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?
shf301
These requirements apply to any multi-threaded application.
Ben Voigt
@Ben Voigt - He's talking about cross-process synchronization of access to the shared memory. It doesn't happen/isn't necessary/happens in a different way e.g. when 2 processes communicate via a pipe or sockets or LPC, because in that case each process has access only to its own memory.
ChrisW
@ChrisW: Absolutely. I'm just saying these issues aren't peculiar to shared memory IPC: they're the same synchronization issues that anyone thinking of writing a multi-process application hopefully has already encountered in multi-threading.
Ben Voigt