- Trigger Device Kernel Object
Disadvantage: The same device kernel object can be read and write multiple times, so that the first completion of the device kernel object will be triggered, so this way can not be used in this situation
voidTest1 () {HANDLE hfile=:: CreateFile (_t ("Aaa.txt"), Generic_read, File_share_read, NULL, open_existing, file_flag_overlapped, NULL); if(!hfile) {Wcout<<l"CreateFile Failed Errocode:"<<::getlasterror () <<Endl; return ; } DWORD dwfilesize=:: GetFileSize (hfile,0); Wcout<<l"FileSize:"<<dwFileSize<<Endl; Char* Pfilecontent =New Char[10000000]; DWORD dwreaded=0; OVERLAPPED O_read= {0}; DWORD Breaddone=:: ReadFile (hfile, Pfilecontent,10000000, &dwreaded,&o_read); DWORD dwerror=:: GetLastError (); if(!breaddone && (dwerror = =error_io_pending)) {WaitForSingleObject (hfile,infinite); Breaddone=TRUE; } if(breaddone) wcout<<l"I/o Code:"<<o_Read.Internal<<"transedbytes:"<<o_Read.InternalHigh<<Endl; ElseWcout<<"Error:"<<::getlasterror () <<Endl; :: CloseHandle (hfile); delete [] pfilecontent;}
- Trigger Event Kernel Object
voidTest2 () {HANDLE hfile=:: CreateFile (_t ("Aaa.txt"), Generic_write, File_share_read, NULL, open_existing, file_flag_overlapped, NULL); if(!hfile) {Wcout<<l"CreateFile Failed Errocode:"<<::getlasterror () <<Endl; return ; } DWORD dwfilesize=:: GetFileSize (hfile,0); Wcout<<l"FileSize:"<<dwFileSize<<Endl; Large_integer Lidis= {0}; Large_integer Liret= {0}; :: SetFilePointerEx (Hfile,lidis,&liret,file_end); Wcout<<l"prewrite File Pos:"<<liRet.LowPart<<Endl; Char* Pfilecontent =New Char[10000000]; memset (Pfilecontent,'Z',10000000); DWORD dwreaded=0; OVERLAPPED O_write= {0}; O_write.offset=Liret.lowpart; O_write.hevent=:: CreateEvent (Null,false,false,null); DWORD Breaddone=:: WriteFile (hfile, Pfilecontent,10000000, &dwreaded,&o_write); DWORD dwerror=:: GetLastError (); if(!breaddone && (dwerror = =error_io_pending)) {WaitForSingleObject (o_write.hevent,infinite); Breaddone=TRUE; } if(breaddone) wcout<<l"I/o Code:"<<o_Write.Internal<<"transedbytes:"<<o_Write.InternalHigh<<Endl; ElseWcout<<"Error:"<<::getlasterror () <<Endl; :: CloseHandle (hfile); delete [] pfilecontent;}
- Can alert io
voidTest3 () {//can alert ioHANDLE hfile =:: CreateFile (_t ("Aaa.txt"), Generic_write, File_share_read, NULL, open_existing, file_flag_overlapped, NULL); if(!hfile) {Wcout<<l"CreateFile Failed Errocode:"<<::getlasterror () <<Endl; return ; } DWORD dwfilesize=:: GetFileSize (hfile,0); Wcout<<l"FileSize:"<<dwFileSize<<Endl; Large_integer Lidis= {0}; Large_integer Liret= {0}; :: SetFilePointerEx (Hfile,lidis,&liret,file_end); Wcout<<l"prewrite File Pos:"<<liRet.LowPart<<Endl; Char* Pfilecontent =New Char[10000000]; memset (Pfilecontent,'g',10000000); DWORD dwreaded=0; OVERLAPPED O_write= {0}; O_write.offset=Liret.lowpart; DWORD Breaddone=:: WriteFileEx (hfile, Pfilecontent,10000000, &O_write, Fileiocompletionroutine); :: CloseHandle (hfile); SleepEx (10000, TRUE); delete [] pfilecontent;}
The advantages and disadvantages of IO can be reminded:
(1) Because of the reason of the callback function, we finally have to put a lot of information in the global variable. Make your code more complex
(2) The request thread and the completion process must be the same thread, and the thread load balancer is not reached
Can alert IO correlation function
(1) QUEUEUSERAPC function
A. This function allows us to add APC items manually.
B. You can force a thread to exit the wait state such as WaitForSingleObjectEx the following is the sample code
VOID WINAPI apcfunc (ulong_ptr pvparam) {// nothing to do}uint WINAPI threadfunc (PVOID pvparam) {wcout<<l"start Wait ...."<<Endl; DWORD DW=:: WaitForSingleObjectEx (Pvparam,infinite,true); if(DW = =wait_object_0) {Wcout<<l"Event signaled"<<Endl; return 0; } Else if(DW = =wait_io_completion) {Wcout<<l"QUEUEUSERAPC forced us out of a wait state"<<Endl; return 0; } return 0;}voidTest4 () {//use QUEUEUSERAPC to stop thread waitingHANDLE hevent =:: CreateEvent (Null,false,false,null); HANDLE Hthread= (HANDLE) _beginthreadex (NULL,0, Threadfunc,hevent,0, NULL); Sleep ( the); QUEUEUSERAPC (Apcfunc,hthread,null); WaitForSingleObject (Hthread,infinite); CloseHandle (Hthread); CloseHandle (hevent);}
- I/O completion port
Cond
Asynchronous device IO "Windows core Programming" chapter 10th study < to be continued >