Re: memory mapped file question
- From: "Jim Mack" <jmack@xxxxxxxxxxxxxxx>
- Date: Thu, 22 Mar 2007 06:53:20 -0400
rainer b. kranok wrote:
I have studied some mmf examples and only found those kind where a
source copies its data to a special address and a target can read out
data from this special memory address and has to copy it into its
local variables for subsequent processing. Of course, I could live
with that, but I wonder if there is no "smoother" way which allows
the target to deal only with standard variables or arrays of
variables which are updated in the background without the need to
explicitely call an update method to read data from the mmf file and
to copy them into the target array.
There may be. Using a MMF backed by the paging file, two processes can open the same memory for sharing -- this much you apparently have seen.
If the realtime process is generating data in that memory, you can overlay a VB array onto that space in the consuming process, and treat it as though it were being updated live. There may be synchronization issues, but I'll assume you can deal with those. One simple method is to reserve one array element as a flag: the generator sets the flag when data is complete, and the consumer polls that location and resets the flag.
The basic trick is to substitute the memory address of the file mapping for the data address of a VB array. You first create the VB array to have the same type (byte, single, UDT, etc) and shape (dimensions & bounds) as the data in the generating process. Then you switch out the addresses, making sure to preserve the original for shutdown. This array must remain in scope, so using a Global Static array in a module is best.
For an example of how to do this, which you'll probably have to modify to suit your data type, etc, is from Karl Peterson: http://vb.mvps.org/samples/MapFile
I haven't studied it, but I believe it'll show you everything you need.
Our Stamina library has a commercial solution, written in ASM, that handles all the details for you. It allows any number of processes to share a block of data, overlaying a VB array of any type (or multiple types).
--
Jim Mack
MicroDexterity Inc
www.microdexterity.com
For example, I would like to have something like this:.
Type MyArrayType
myVar as Integer
End Type
dim MyArray() as Type MyArrayType
Sub AnySub ' timer or event driven
::::::
myVar = MyArray(I).myVar
doSomethingWith myVar
:::
End Sub
where MyArray is continuously updated by the source application,
without the need to have to call first a special sub which copies the
mmf data, interprets them and fills them into my array. Again, I
surely would be able to write an event driven code in plain VB which
would do that, but I would like to know if there is a built in
mechanism or API or somewhat else available which does this job for
me, hopefully in a more efficient way than I can do with plain VB. I
have to deal with realtime process data, and there I have to avoid
any overhead as far as possible.
An additional question would be if such a mechanism is available in
an other language like C++ or elsewhere?
Thanks for reading this, and any answers.
- References:
- memory mapped file question
- From: rainer b. kranok
- memory mapped file question
- Prev by Date: Re: memory mapped file question
- Next by Date: Re: [MULTIPOST] Capturing contents from socket
- Previous by thread: Re: memory mapped file question
- Index(es):
Relevant Pages
|