I'm new to memory mapping, what I want to do is to share a map file between many threads, for that I need to create the map file and use the function: MapViewOfFile
so every thread can access to a part of the file, of course I need to send the offset of the view to each thread that respects allocation granularity. But the part that I don't understand is: dwFileOffsetHigh & dwFileOffsetLow.
MSDN says:
The combination of the high and low offsets must specify an offset within the file mapping.
So how can I set the values of these two parameters in a way that they can specify the right offset. Do I need to make any calculations or just use variables and the system handles the rest (Finding the offset) ?, I'm really stuck with this, and every time I make a try I get an exception. So assuming that I know the offset and the size of each view, how can I possibly know the values of these too parametres? An example is worth a thousand explanations. And here is an explanation of what I'm trying to do:
// The main thread create map file and specify the view for every worker thread:
WorkerThreads[i] := WorkerThread.create(...,bloc_offset,bloc_size,...); // So each worker writes in a specified view.
//The worker thread then opens the view and writes data in:
data := mapViewOfFile(mapfileH, FILE_MAP_WRITE, dwFileOffsetHigh, dwFileOffsetLow, blocSize);`
Thanks for answering.
If your file is <= 2GB in size, you can pass the desired offset to each thread as a DWORD
and then each thread can assign its offset directly to dwFileOffsetLow
and set dwFileOffsetHigh
to 0.
pView := MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, offset, size);
If your file is > 2GB in size, pass the desired offset to each thread as an Int64
or UInt64
, and then use a ULARGE_INTEGER
variable to break up the value into its low and high components, which can then be assigned to dwFileOffsetLow
and dwFileOffsetHigh
.
var
ul: ULARGE_INTEGER;
ul.QuadPart := offset;
pView := MapViewOfFile(hMapping, FILE_MAP_WRITE, ul.HighPart, ul.LowPart, size);