1

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.

Mike Torrettinni
  • 1,816
  • 2
  • 17
  • 47
Safa
  • 485
  • 2
  • 5
  • 24
  • Are you mapping a physical file or just a memory-backed buffer? If the latter and/or mapping the entire file you would typically just pass zeroes for file offset. – 500 - Internal Server Error Feb 26 '16 at 20:07
  • No, I said it's a multithreaded application, so each thread writes data starting from a different offset, if I pass zeroes for file offset, every thread will write data to offset 0. I'm mapping a physical file. – Safa Feb 26 '16 at 20:38
  • That's not how I would recommend doing it. IMO, you should use the same mapping in all threads - you can even use the same `data` pointer - just offset it in each thread. E.g. thread1: data[0]..., thread2: data[10000]..., etc. – 500 - Internal Server Error Feb 26 '16 at 20:45

1 Answers1

1

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);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks, this works fine for me. A last question, is it okay to implement the second method ( ULARGE_INTEGER), in both cases : file <= 2GB and file > 2GB, because I'm dealing with files from different sizes. Or should I get the file size first and decide which method to work with? – Safa Feb 27 '16 at 06:32
  • You can use `ULARGE_INTEGER`. For sizes less than 2GB, its `HighPart` will be 0. – Remy Lebeau Feb 27 '16 at 15:54