异步IO写在追加文件的时候,基本上属于同步速度执行,writefile立即返回是个随机事件
解决方法,先把文件设置为目的大小,然后写入各块数据
一般处理方法:先设置成各大尺寸的文件,填入数据设置文件结尾即可以okokok
// 异步IO测时.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "windows.h"
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
/*
cout << "open the file : ";
string strfl1;
cin >> strfl1;
cout << "create the file : ";
string strfl2;
cin >> strfl2;
HANDLE hfile1 = CreateFile(strfl1.c_str(), GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hfile1 == INVALID_HANDLE_VALUE)
{
cout << "createfile1" << endl;
return 1;
}
HANDLE hfile2 = CreateFile(strfl2.c_str(), GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
if (hfile2 == INVALID_HANDLE_VALUE)
{
cout << "createfile2" << endl;
return 1;
}
BYTE *pbuffer = new BYTE[1024 * 1024 * 80];
if (!pbuffer)
{
cout << "new" << endl;
return 2;
}
ZeroMemory(pbuffer, 1024 * 1024 * 80);
DWORD dwReaded = 0;
BOOL bisOkR = ReadFile(hfile1, pbuffer, 1024 * 1024 * 80, &dwReaded, NULL);
if (!bisOkR)
{
cout << "ReadFile" << endl;
CloseHandle(hfile1);
CloseHandle(hfile2);
delete []pbuffer;
return 3;
}
OVERLAPPED ovelap;
ZeroMemory(&ovelap, sizeof(ovelap));
ovelap.Offset = 0;
ovelap.OffsetHigh = 0;
ovelap.hEvent = NULL;
BOOL bIsOkW, bWaitForComplete;
DWORD dwtransferredbytes = 0;
DWORD dwWritedBytes = 0;
DWORD dws = GetTickCount();
// the first data
bIsOkW = WriteFile(hfile2, pbuffer, 1024l * 1024 * 10, &dwWritedBytes, &ovelap);
if (!bIsOkW && ::GetLastError() != ERROR_IO_PENDING)
{
cout << "writefile0" << endl;
CloseHandle(hfile1);
CloseHandle(hfile2);
delete []pbuffer;
return 4;
}
DWORD dwe = GetTickCount();
cout << "block 0 : " << dwe - dws << " ms" << endl;
for (int i = 1; i != 8; ++ i)
{
dws = GetTickCount();
bWaitForComplete = GetOverlappedResult(hfile2, &ovelap, &dwtransferredbytes, TRUE);
if (!bWaitForComplete)
{
cout << GetLastError() << endl;
}
dwe = GetTickCount();
cout << " wait block " << i - 1 << " : " << dwe - dws << " ms" << endl;
memset(&ovelap, 0, sizeof(OVERLAPPED));
ovelap.Offset = 1024l * 1024 * 10 * i;
ovelap.OffsetHigh = 0;
dws = GetTickCount();
bIsOkW = WriteFile(hfile2, pbuffer + 1024l * 1024 * 10 * i, 1024l * 1024 * 10, &dwWritedBytes, &ovelap);
if (!bIsOkW && ::GetLastError() != ERROR_IO_PENDING)
{
cout << "writefile" << i << endl;
CloseHandle(hfile1);
CloseHandle(hfile2);
delete []pbuffer;
return 4;
}
dwe = GetTickCount();
cout << "block " << i << " : " << dwe - dws << " ms" << endl;
}
//the last data
dws = GetTickCount();
bWaitForComplete = GetOverlappedResult(hfile2, &ovelap, &dwtransferredbytes, TRUE);
if (!bWaitForComplete)
{
cout << GetLastError() << endl;
}
dwe = GetTickCount();
cout << " wait block 8 : " << dwe - dws << " ms" << endl;
delete []pbuffer;
cout << "okokok" << endl;
return 0;
*/
cout << "open the file : ";
string strfl1;
cin >> strfl1;
cout << "create the file : ";
string strfl2;
cin >> strfl2;
HANDLE hfile1 = CreateFile(strfl1.c_str(), GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hfile1 == INVALID_HANDLE_VALUE)
{
cout << "createfile1" << endl;
return 1;
}
HANDLE hfile2 = CreateFile(strfl2.c_str(), GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
if (hfile2 == INVALID_HANDLE_VALUE)
{
cout << "createfile2" << endl;
return 1;
}
HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, "WTEEVT");
if (!hEvent)
{
cout << "createevent" << endl;
return 2;
}
BYTE *pbuffer = new BYTE[1024 * 1024 * 80];
if (!pbuffer)
{
cout << "new" << endl;
return 3;
}
ZeroMemory(pbuffer, 1024 * 1024 * 80);
DWORD dwReaded = 0;
BOOL bisOkR = ReadFile(hfile1, pbuffer, 1024 * 1024 * 80, &dwReaded, NULL);
if (!bisOkR)
{
cout << "ReadFile" << endl;
CloseHandle(hfile1);
CloseHandle(hfile2);
delete []pbuffer;
return 4;
}
OVERLAPPED ovelap;
ZeroMemory(&ovelap, sizeof(ovelap));
ovelap.Offset = 0;
ovelap.OffsetHigh = 0;
ovelap.hEvent = hEvent;
BOOL bIsOkW;
DWORD dwsigl = 0;
DWORD dwWritedBytes = 0;
DWORD dws = GetTickCount();
// the first data
bIsOkW = WriteFile(hfile2, pbuffer, 1024l * 1024 * 10, &dwWritedBytes, &ovelap);
if (!bIsOkW && ::GetLastError() != ERROR_IO_PENDING)
{
cout << "writefile0" << endl;
CloseHandle(hfile1);
CloseHandle(hfile2);
delete []pbuffer;
return 5;
}
DWORD dwe = GetTickCount();
cout << "block 0 : " << dwe - dws << " ms" << endl;
for (int i = 1; i != 8; ++ i)
{
dws = GetTickCount();
dwsigl = WaitForSingleObject(hEvent,INFINITE);
if (dwsigl != WAIT_OBJECT_0)
{
cout << GetLastError() << endl;
}
dwe = GetTickCount();
cout << " wait block " << i - 1 << " : " << dwe - dws << " ms" << endl;
ovelap.Offset = 1024l * 1024 * 10 * i;
ovelap.OffsetHigh = 0;
dws = GetTickCount();
bIsOkW = WriteFile(hfile2, pbuffer + 1024l * 1024 * 10 * i, 1024l * 1024 * 10, &dwWritedBytes, &ovelap);
if (!bIsOkW && ::GetLastError() != ERROR_IO_PENDING)
{
cout << "writefile" << i << endl;
CloseHandle(hfile1);
CloseHandle(hfile2);
delete []pbuffer;
return 5;
}
dwe = GetTickCount();
cout << "block " << i << " : " << dwe - dws << " ms" << endl;
}
//the last data
dws = GetTickCount();
dwsigl = WaitForSingleObject(hEvent,INFINITE);
if (dwsigl != WAIT_OBJECT_0)
{
cout << GetLastError() << endl;
}
dwe = GetTickCount();
cout << " wait block 8 : " << dwe - dws << " ms" << endl;
delete []pbuffer;
cout << "okokok" << endl;
return 0;
}