I'm trying Windows overlapped IO but I can't seem to get it to work asynchronously. I've compiled and run the program below but it never prints anything, it just completes silently. I've read small reads could become synchronous, that's why I deliberately chose to read 512MB.
const DWORD Size = 1<<29; // 512MB
char* Buffer = (char*)malloc(Size);
DWORD BytesRead;
OVERLAPPED Overlapped;
memset(&Overlapped, 0, sizeof(Overlapped));
HANDLE File = CreateFile("BigFile", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, NULL);
assert(File!=INVALID_HANDLE_VALUE);
DWORD Result = ReadFileEx(File, Buffer, Size, &Overlapped, NULL); // This line takes 150ms according to the debugger
assert(Result);
while(!GetOverlappedResult(File, &Overlapped, &BytesRead, FALSE)) {
printf("Waiting...\n");
}
As additional information, I've stepped the code into the debugger, and the Overlapped.InternalHigh
value gets updated (with the same value as Size
) during the ReadFileEx
call.
I've tried replacing malloc
with VirtualAlloc
, ReadFileEx
with ReadFile
, adding FILE_FLAG_NO_BUFFERING
, and checked that the return of ReadFile
was 0 and that GetLastError
would be ERROR_IO_PENDING
after the read. I've tried using RAMMap to see if the file was in cache but only 96KB of it was there.
I'm running Windows 10 (ver. 1703) with 8GB RAM.