APC mechanism thread

When the system creates a thread of time, it will also create a queue associated with the thread. This queue is called asynchronous procedure call (APC) queue.

To the thread APC queue entries are processed, the thread must be set to remind themselves state, simply means that we threads during execution has reached a point, at which point it can handle interrupted case, the thread below the six functions can be set to remind the state:

SleepEx BOOL ();
DWORD the WaitForSingleObjectEx ();
DWORD WaitForMultipleObjectsEx ();
BOOL SingalObjectAndWait ();
BOOL GetQueuedCompletionStatusEx (); // This last parameter is a function of five BOOL value indicating whether the state is set to be alert.

DWORD MsgWaitForMultipleObjectsEx (); // last parameter is set to sign MWMO_ALERTABLE

When one of our top six function calls and threads to be reminded when the state, the system will first check the thread APC queue, if there is at least one queue, the system will not let the thread goes to sleep. The system will be taken out and put a thread to perform the associated callback function. When the callback function returns, checks to see if there are other items in the APC queue again, if there is to continue treatment, if not, to call to remind the function will return. This is in front of the APC on the basis of one.
But just when calling the function, APC thread in a queue are not, these functions will be suspended thread. After hangs, if you call a function to the thread's APC queue to add a, the thread will be awakened, executed after the callback queue associated with that item and the item removed from the queue, then wait function returns immediately.
Microsoft provides a function for us, can be a manually added to the APC queue thread.
The QueueUserAPC DWORD (PAPCFUN pfnAPC, HANDLE hThread, a ULONG_PTR of dwParam);
pfnAPC: Pointer to the callback function. Function prototype must look something like this: VOID WINAPI APCFUN (ULONG_PTR dwParam);
hThread: address space handle to the thread, if it is another thread in the process, then pfnAPC function memory address must be located in another thread.
dwParam: parameters passed to the callback function.

This can be used a very efficient communication between threads, but can only pass one value.

This function can also be forced to let the thread exit. Join waiting function is waiting for a kernel object when the kernel object has not been triggered, we may terminate stronger thread or process, so that the thread is forced to exit the "clean" thread exits.

eg:
If an existing main thread, the main thread creates a child thread, the child thread calls WaitForMultipleObjectEx () and threads to be reminded state, if the user to immediately terminate the application:

VOID WINAPI APCFunc(ULONG_PTR dwParam)
{//Nothing To Do};

WINAPI of ThreadFunc int (PVOID pvParam)
{
        DWORD DW = the WaitForSingleObjectEx (the hEvent, of INFINITE, TRUE);
       IF (DW == WAIT_OBJECT_0) {} // wait event was successful
       if (dw == WAIT_TO_COMPLETION) {return (0);} // there are entries in the queue APC, after the implementation to perform this place APCFun return (0) normal thread will end
       ..........
       return (0);
   }
VOID main ()
{
        HADNLE the CreateEvent the hEvent = () ;
       HANDLE = hThread the CreateThread (NULL, 0, of ThreadFunc, & the hEvent, NULL, NULL); A
       the QueueUserAPC (APCFunc, hThread, NULL);
       the WaitForSingleObject (hThread, of INFINITE);
       CloseHadle (hThread);
       the CloseHandle (the hEvent);
   }

Of course, to create another kernel event object by calling WaitForMultipleObjects () can terminate the thread, but WaitForMultipleIbjects () while waiting for all objects are triggered, this is the only thread exit.

Published 37 original articles · won praise 12 · views 9231

Guess you like

Origin blog.csdn.net/weixin_43265881/article/details/104089417