http://bbs.youkuaiyun.com/topics/330091476
1. apc调用在线程开始, 解决法子: queueuserapc之前sleep一下。
2. apc 的一个函数 sleepex 用法错误
http://bbs.youkuaiyun.com/topics/390873092
VOID WINAPI APCFunc2(ULONG_PTR dwParam)
{
for(int i = 0; i < 10; i++)
printf("APCFunc2\n");
}
DWORD WINAPI ThreadFun(PVOID pvParam)
{
HANDLE hEvent = (HANDLE)pvParam;
printf("%d\n", GetCurrentThreadId());
for(int i = 0 ; i < 10; i++)
printf("SleepEx \n");
SleepEx(50,TRUE);
for(int i = 0 ; i < 10; i++)
printf("1111 \n");
SleepEx(3000,FALSE);
return 0;
}
int main(int argc, char* argv[])
{
HANDLE hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
HANDLE hThread = (HANDLE)_beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))ThreadFun,(PVOID)hEvent,0,NULL);
Sleep(2000);
QueueUserAPC(APCFunc2,hThread,NULL);
WaitForSingleObject(hThread,INFINITE);
CloseHandle(hEvent);
hEvent= NULL;
CloseHandle(hThread);
hThread = NULL;
system("pause");
return 0;
}
QueueUserAPC(APCFunc2,hThread,NULL); 2秒钟后才调用,而SleepEx(50,TRUE); 才50毫秒中,
SleepEx(3000,FALSE); 的最后一个参数错误。
SleepEx
bAlertable [in]
If this parameter is FALSE, the function does not return until the time-out period has elapsed. If an I/O completion callback occurs, the function does not return and the I/O completion function is not executed. If an APC is queued to the thread, the function does not return and the APC function is not executed.
If the parameter is TRUE and the thread that called this function is the same thread that called the extended I/O function (ReadFileEx or WriteFileEx), the function returns when either the time-out period has elapsed or when an I/O completion callback function occurs. If an I/O completion callback occurs, the I/O completion function is called. If an APC is queued to the thread (QueueUserAPC), the function returns when either the timer-out period has elapsed or when the APC function is called.
所以,在用apc的时候,不要用false.
另外: apc会不会在线程结束的时候“剩下没有执行的”呢?
不会,请看:After the thread calls an APC function, it calls the APC functions for all APCs in its APC queue. 清空apc队列后才会返回。