Asynchronous device IO "Windows core Programming" chapter 10th study < to be continued >

Source: Internet
Author: User

Asynchronous IO operations differ from synchronous operations:
    1. The file_flag_overlapped sign in CreateFile.
    2. Asynchronous operation function lpoverlapped parameter
Receive IO Request completion notification
  1. 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;}
  2. 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;}
  3. 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);}
  4. I/O completion port
    Cond

Asynchronous device IO "Windows core Programming" chapter 10th study < to be continued >

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.