MemoryMappedViewAccessor c++ analog - c++

I'm looking for MemoryMappedViewAccessor С++ analog. It exists?
I want to send data from C++ app to .net app using shared memory. And I need random access. Is it possible?
Thank you!

There is no equivalent in C++. Mostly because none is needed, you can simply access the shared memory with a pointer. MemoryMappedViewAccessor is required to serialize garbage collected objects to the view.
Which makes shared memory pretty inefficient from managed code. Do make sure that a pipe or a socket doesn't solve your problem first. They usually do, dealing with concurrency is also much easier with them. Shared memory requires a raft of named mutexes to arbitrate access.

Related

Write boost managed shared memory to file

I hope any expert using the boost managed shared memory can help me. I´m trying to write the memory to a file. I can not figure it out with the boost examples. ¿Can anyone provide me some examples?
Thanks.
If you truly need this, I see roughly 2 approaches, at a glance:
Copy it
use serialization/deserialization
or just copy by constructing clones in a different segment manager (obviously linked to a memory mapped file, this time)
Use a Managed External Buffer. A managed buffer is basically your segment manager on top of some transparent memory buffer. You can decide whether it exists in local process address space, shared memory, or indeed a memory-mapped file.
This is the supported method to use the same segment manager + segment data layout in both.
If you're really desperate you COULD try to bitwise copy the complete shared memory object into a file of equal size and simply open it. This might work IFF the managed_mapped_file implementation has exactly the same or compatible segement management structure, headers and layout. That's a long call though, and even if it appears to work, it's at best undocumented, and therefore likely invokes undefined behaviour.
Perhaps you are looking for mapped_file: http://www.boost.org/doc/libs/1_63_0/libs/iostreams/doc/classes/mapped_file.html
It is a memory-mapping API for files, and you can open the same file in multiple processes.

Using Shared Memory with PLCSIM respectively with non-shared-memory-prepared applications

I want to use shared memory between two applications. In this case between a c++ main-application and PLCSIM, a virtual programmable logic controller. Anyway I did not know if PLCSIM is prepared for shared memory usage. Is it possible to force PLCSIM to make it memory storage available for other processes like shown in the MSDN-example related to a main-main-interprocess-communication example: http://msdn.microsoft.com/en-us/library/windows/desktop/aa366551(v=vs.85).aspx
Im not sure what your end goal is but could you look at using NettoPLCSim. It makes use of a "COM-Interface of PLCSim to read/write the data out of it". You could use source code to make an application to store data to where you require to share with your C++ code
http://nettoplcsim.sourceforge.net/

Transfering data between threads in C++ and Fortran

I need to move large amounts (~10^6 floats) between multiple c++ threads and a fortran thread. At the moment we use windows shared memory to move very small piece of data, mainly for communication, and then save the file to a proprietary format to move the data. I've been asked to look at moving the bulk of the data via shared memory, but looking at the shared memory techniques in windows (seemingly a character buffer) this looks like a mess. Another possibility is boost's interprocess communication, but not sure how to use that from fortran, or if it's a good idea. Another idea was to use a database like sqlite.
I'm just wondering if anyone had any experience or would like to comment, as this is a little over my head at the moment.
Thanks very much
Jim
Use pipes. If you can inherit handles between processes, you can use anonym pipes, when not, you have to use named pipes. Also, threads share the address space, so you're probably thinking of processes when you say threads.

How to implement a shared buffer?

I've got one program which creates 3 worker programs. The preferable method of communication in my situation would be through a memory buffer which all four programs may access.
Is there a way to pass a pointer, reference or any kind of handler to the child processes?
Update
The three child programs are transforming vertex data while the main program primarily deals with UI, system messages, errors, etc..
I'm hoping there is some way to leverage OpenCL such that the four programs can share a context. If this is not possible, it would be nice to have access to the array of vertices across all programs.
I suppose our target platform is Windows right now but we'd like to keep it as cross-platform as possible. If there is no way to implement this utilizing OpenCL we'll probably fall back to wrapping this piece of code for a handful of different platforms.
Your question is platform dependent, therefore :
for Windows : Named Shared Memory
for linux : mmap or POSIX shared memory access
general case : boost::interprocess
If you explain a bit what kind of data is shared and other constraints/goal of the system it would be easier to answer your question.
I wonder why you think a shared buffer would be good? Is that because you want to pass a pointer in the buffer to the data to be worked on? Then you need shared memory if you want to work across processes.
What about a client-server approach where you send data to clients on request?
More information about your problem helps giving a better answer.
You should use Named Shared Memory and inter-process synchronization.
This is somewhat wider than the original question on shared memory buffers, but depending on your design, volume of data and performance requirements you could look into in-memory databases such as Redis or distributed caches, especially if you find yourself in 'publish-subscribe' situation.

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++?
You should check out Boost.Interprocess. It provides the functionality you need and the documentation contains instructions on how to use it.
There are some issues you need to think of when using shared memory:
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).
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?
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.
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.
If you're looking for a simple example, Using Shared Memory in a Dynamic-Link Library from MSDN is probably a good starting point.