APC异步调用过程二
转自:http://blog.sina.com.cn/s/blog_6c617ee301017nhr.html
2014-08-28 17:15:52
#include <windows.h>
#include <process.h>
using namespace std;
{
cout<<"APCFunc:处理APC函数ing"<<endl;
Sleep(2000);
cout<<"APCFunc:Sleep了2000,处理APC函数ing"<<endl;
}
{
cout<<"APCFunc2:处理APC函数2ing"<<endl;
Sleep(100);
cout<<"APCFunc2:处理APC函数2ing"<<endl;
}
{
HANDLE hEvent = (HANDLE)pvParam;
Sleep(100);
cout<<"ThreadFun:WaitForSingleObjectEx(hEvent,INFINITE,TRUE);"<<endl;
DWORD dw = WaitForSingleObjectEx(hEvent,INFINITE,TRUE);
if(dw == WAIT_OBJECT_0)
{
cout<<"ThreadFun:事件触发"<<endl;
}
{
//如果线程至少处理了APC队列中的一项
cout<<"ThreadFun:APC队列中APC函数执行完,等待函数返回WAIT_IO_COMPLETION激活该线程继续执行"<<endl;
}
return 0;
}
{
HANDLE hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
HANDLE hThread = (HANDLE)_beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))ThreadFun,(PVOID)hEvent,0,NULL);
//执行一些其他代码
cout<<"main:开始"<<endl;
//此时用户想要终止子线程
Sleep(1000); //留出时间先让子线程进入alertable状态。
cout<<"main:主程Sleep(1000)此时线程调用WaitForSingleObjectEx处于alertable状态"<<endl;
QueueUserAPC(APCFunc,hThread,NULL);
cout<<"main:QueueUserAPC(APCFunc,hThread,NULL);"<<endl;
Sleep(1500);
cout<<"main:主程Sleep了1500时间比APCFunc的2000短,让线程在执行完APC函数前往其中添加新的APCFunc2"<<endl;
QueueUserAPC(APCFunc2,hThread,NULL);
cout<<"QueueUserAPC(APCFunc2,hThread,NULL);"<<endl;
WaitForSingleObject(hThread,INFINITE);
cout<<"main:WaitForSingleObject(hThread,INFINITE);"<<endl;
system("pause");
return 0;
}
main()在Sleep(1000); 的时候确保新线程调WaitForSingleObjectEx进入alertable状态以执行APC函数。应为返回dw的不是WAIT_OBJECT_0所以不是hEvent被激活触发,而是APC队列中函数执行完毕触发的。
WaitForSingleObjectEx函数原型如下
DWORD WINAPI WaitForSingleObjectEx(
__in HANDLE hHandle,
__in DWORD dwMilliseconds,
__in BOOL bAlertable
);
在SDK6.1文档中,对参数dwMilliseconds说明如下:
The time-out interval, in milliseconds. The function returns if the interval elapses, even if the object's state is nonsignaled and no completion routines or APCs are queued.If dwMilliseconds is zero, the function tests the object's state and checks for queued completion routines or APCs and then returns immediately. IfdwMilliseconds is INFINITE, the function's time-out interval never elapses.
我对其中标红语句的理解是,如何超过时间间隔,即使(even if)对象信号未被触发并且没有完成例程或者APC例程排队。我觉得比较难翻译及理解。于是我又查了下网上MSDN,对参数dwMilliseconds说明如下:
The time-out interval, in milliseconds. If a nonzero value is specified, the function waits until the object is signaled, an I/O completion routine or APC is queued, or the interval elapses. IfdwMilliseconds is zero, the function does not enter a wait state if the criteria is not met; it always returns immediately. IfdwMillisecondsis INFINITE, the function will return only when the object is signaled or an I/O completion routine or APC is queued.
还是没有搞清楚若超过时间间隔,但是当前有完成例程或者APC例程,函数是否返回,于是写了个小Demo测试了一下
#include "stdafx.h" #include <Windows.h> #include <iostream> using namespace std; VOID NTAPI ApcFun(__in ULONG_PTR Parameter) { cout<<"ApcFun Running."<<endl; Sleep(10000); } int _tmain(int argc, _TCHAR* argv[]) { HANDLE hEvent=CreateEvent(NULL,FALSE,FALSE,_T("")); DWORD dwRet=QueueUserAPC(ApcFun,GetCurrentThread(),NULL); cout<<dwRet<<endl; dwRet=QueueUserAPC(ApcFun,GetCurrentThread(),NULL); cout<<dwRet<<endl; dwRet=WaitForSingleObjectEx(hEvent,1000,TRUE); cout<<dwRet<<endl; CloseHandle(hEvent); return 0; }
测试结果是即使超时时间已到,若当前存在完成例程或者APC例程,会把所有的例程执行完毕后才会返回。
如果WaitForSingleObjectEx第3个参数为TRUE,那么WaitForSingleObjectEx在执行时发现存在APC例程需要执行,则当前等待线程执行APC过程,执行完毕后,
不管同步对象是否有信号或者是否超时,函数返回,返回值为WAIT_IO_COMPLETION。
我写个小demo供测试
QueueUserAPC执行完毕后子线程还在初始化中,此时若发现APC队列中存在APC例程,则会先执行apc队列,然后再执行
线程函数,这样会造成WaitForSingleObjectEx执行是apc队列是空的,达不到测试的目的,
就算APC执行过程中同步对象已然有信号,WaitForSingleObjectEx已然会返回WAIT_IO_COMPLETION
浙公网安备 33010602011771号