Reprinted: http://www.vchelp.net/ Original author's name fang (fangguicheng@21cn.com) body asynchronous IO, APC, IO completed port, thread pool and high performance server two APC
Alertable IO provides a more efficient asynchronous notification form. ReadFileEx / WriteFileEx provides a callback function (APC process) while issuing IO requests. When the IO request is completed, the callback function will be executed once the thread enters the alarm state.
The following five functions enable the thread to enter the alarm status:
Sleepex
WaitforsingleObjectex
WaitFormultiPleObjectsex
SignalObjectandWait
MsgwaitFormultiPleObjectsex
When the thread enters the alarm state, the kernel will check the APC queue of the thread. If there is an APC in the queue, it will be executed in the FIFO mode. If the queue is empty, the thread will hang a wait object. At some point in the future, once the APC enters the queue, the thread will be awakened to execute the APC, while waiting for the function to return to WAIT_IO_COMPLETION.
QueueUSeraPC can be used to deliver APC, as long as the target thread is on the alarm state, the APC can be executed.
The main disadvantage of using the alarm IO is that the thread that issues the IO request must also be a thread of the processing result. If there is an unfinished IO request when a thread exits, then the application will always lose IO to complete the notification. However, we will see that the IO completion port does not have this limit.
The following code demonstrates the usage of QueueUseraPC.
/ ************************************************** ********************** /
/ * APC Test. * /
/ ************************************************** ********************** /
DWORD WINAPI Workthread (Pvoid PPARAM)
{
Handle Event = (Handle) PPARAM;
For (;;)
{
DWORD dwret = WaitforsingleObjectEx (Event, Infinite, True);
IF (dwret == Wait_Object_0)
Break;
ELSE IF (dwret == Wait_io_completion)
Printf ("WAIT_IO_COMPLETION / N");
}
Return 0;
}
Void Callback ApcProc (DWORD DWPARAM)
{
Printf ("% s", (pvoid) dwparam;
}
Void TestAPC (Bool Bfast)
{
Handle Quitevent = CreateEvent (NULL, FALSE, FALSE, NULL);
Handle hthread = CreateThread (NULL,
0,
Workthread,
(PVOID) QUITEVENT,
0,
NULL);
Sleep (100); // Wait for Workthread Initialized.
For (int i = 5; i> 0; I -)
{
QueueUseraPC (APCProc, Hthread, (DWORD) (PVOID) "APC Here / N");
IF (! bfast)
Sleep (1000);
}
Setevent (QUITEVENT); WaitforsingleObject (hthread, infinite);
CloseHandle (HTHREAD);
}
bibliography
1, MSDN LIBRARY
2, "Windows Advanced Programming Guide"
3, "Windows Core Programming"
4, "Windows 2000 Device Driver Design Guide"
Finish