You don't need a mutex. You can safely read and write to the same file in multiple threads. You can open the file more than once so that each thread has its own file handle and thus its own file pointer or you can use overlapped I/O to read or write at a specific position.
You don't need to open the file for overlapped I/O. You can just do this:
LARGE_INTEGER off = offset;
OVERLAPPED ov = { 0 };
ov.Offset = off.LowPart;
ov.OffsetHigh = off.HighPart;
ReadFile(handle, buffer, count, &bytesRead, &ov);
This allows you to read at a particular position in the file (offset
) atomically, so you don't have to call SetFilePointerEx
and worry about another thread messing up the pointer. The same thing works for writing.