C ++ to C # data transfer - mapping file vs CLI + UnmanagedMemoryStream

I need to transfer data from C ++ to C # frequently. It should be as fast as possible as I am writing a low low latency application.

I have a clean C ++ project (will be ported to Linux later). I need to transfer data from my C # project. I always need to pass the same structure (if that's important).

The structure looks like this: Ticker = "MSFT" Price = "30" Volume = "10" Side = "Buy"

I present two options:

  • write C ++ - CLI wrapper that will pass UnmanagedMemoryStream from C ++ to C #
  • use memory mapped files

What will happen:

  • faster
  • easier to implement
  • easier to connect to Linux?
+3


source to share


2 answers


Using mixed C ++ / CLI modules is not supported at all in Mono (see here ). Mono only supports pure C ++ CLR assemblies.
Hence, it seems that other features like memory mapped files, or as Matthew suggested using P / Invoke (which is supported in Mono, although it requires some extra work ) are what you should use if you plan on connecting to Linux ...



+2


source


Memory mapped files are very fast and relatively easy to use. I have used them to transfer a large amount of ECG data from a C # application to a C ++ application. I've also used them to carry around smaller blocks of memory (like structures). It's about as fast as you can get to transfer data between processes. (You will likely need to use a synchronization mechanism, such as a semaphore, to control the read / write of data from each end.)

However, I don't know if they are easy to port to Linux, but I know Linux supports some kind of MMF, so I'm sure it won't be too difficult. But I cannot say for sure.



NOTE. If you want to port data to a C ++ function in a DLL running in a proc, that is a completely different matter and you can just use P / Invoke to do that. Using P / Invoke will be much faster than using MMF (assuming the data can be pinned by the marshaler and doesn't need to be copied). Generally speaking, with P / Invoke, you can simply pass a pointer to a C ++ function.

+1


source







All Articles