Turn from: http://blog.sina.com.cn/s/blog_c2ef450f010192hx.html
APC Asynchronous Procedure Call
I remember the first time I saw this noun, I think the cloud fog hood, but the internet to see the introduction of it, feeling that the words are nothing
Today, however, I touch it again, and I write down my understanding of it, and if it is wrong, please correct me (refer to the fifth edition of Windows core programming) Wolf Shadow---------2013, 10, 18
Okay, cut the crap, let's get to the point:
The first thing we should understand is that when the thread is created, the so-called APC is established and associated with the line threads relative, and the APC is a queue;
Generation of items in queues:
Then who is to add content to the queue, this will be about the device driver, now is not feel the reason why, listen to me slowly; When a thread issues an I/O request (where we are talking about an asynchronous I/O request), the device driver receives its request, Then the thread performs the rest of its work (until at some point it really does not (because it has to be the requested I/O result at this point)), so how does the device driver make the thread until he has finished I/O after executing the I/O request?
That is, it adds an entry to the APC queue, and then the question goes, how does it know to add an entry to the APC queue in this thread? Hey, this is, of course, the thread told him, where to tell, of course, in the sending I/O request to tell, in this I do not sell the lawsuit, said directly, the thread in the request i/ O operation is ReadFileEx () or WriteFileEx () function, when sending an I/O request, the two functions tell the device driver, "Hey, buddy, when you're done with my request, send the notice to my APC queue," he said. (The content of this queue item, of course, is the address of the completion function and the address of the overlapped structure when I/O requests it (as to what is the completion function and the overlapped structure, I think I will not say more)),
Now that we know the origin of the items in the APC queue, the next thing we need to know is the processing of the items:
When an item is added to the APC queue, it is not processed immediately (the thread cannot be interrupted in the event that other tasks are handled) in order to process the item in the thread APC queue, the thread must set itself to be a reminder state.
So what is a reminder state, and how can you set it as a reminder?
Alert state in my sense that he was the concept of dealing with the items in the APC, because in the case of an item in the queue, the thread handles those items, and when there is no entry in the APC queue, the thread is suspended; (it seems to me that the reminder here should be the meaning of the notice, is to notify APC that there is an entry in the queue to process it. Remember. The thread will only suspend if and only if one of the threads ' APC queues does not
As for how to make a thread a reminder state, Windows provides us with 6 functions
SleepEx ()
WaitForSingleObjectEx ()
Waitformultiobjectex ()
SignalObjectAndWait ()
Getqueuedcompletionstatusex ()
Msgwaitformultipleobjectex ()
The last parameter in the first five functions is a bool value, indicating whether the calling thread should set itself to be a reminder state
For the last parameter of the last function, we should use the MWMO_ALERTABLE flag to allow the thread to enter the alert state
Also, when a thread is suspended, it wakes up by waiting for the kernel object to fire, or an entry in the APC queue, and when an item appears, the system wakes up our thread (through the callback function) and empties the queue. The function then returns immediately---the thread does not sleep again to wait for the kernel object to be triggered