用户模式下apc的几个错误用法

http://bbs.csdn.net/topics/330091476


1.  apc调用在线程开始, 解决法子: queueuserapc之前sleep一下。


2. apc 的一个函数 sleepex 用法错误


http://bbs.csdn.net/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队列后才会返回。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值