APC相关知识

  • APC的理解

每个线程除了有单独的消息队列,还有一个APC队列(用来保存等待执行的APC函数);如果线程进入alertable状态时,发现APC队列中有情况,就跳过去执行,执行完毕后再回来接着处理消息队列。

线程调用过以下函数并且bAlertable这个标志被设置为True的时候才会使其处于Wait状态

能进入APC队列的回调函数和其他回调函数还有一个很大的不同:

可以参见

http://www.3k2.cn/article/Thread/78.html

的说明:

要进入 APC 队列只有 SetWaitableTimer 的调用还不够, 还要通过 "等待函数" 介绍一下,可以介绍 APC 入列的等待函数:
 SleepEx();
WaitForSingleObjectEx();
WaitForMultipleObjectsEx();
MsgWaitForMultipleObjectsEx();
SignalObjectAndWait();

 
  • APC的定义

在windows internals forth edition中对asynchronous procedure call 的定义为:

A function that provides a way for user programs and system code to execute code in the context of a particular user thread (and hence a particular process address space).An APC can be either kernel mode or user mode. (Kernel-mode APCs don't require "permission" from a target thread to run in that thread's context, as user-mode APCs do.)

一个函数,为用户程序或者系统代码提供一种在特定用户线程中执行代码的方法。

  • APC queue的定义

A queue in which asynchronous procedure calls (APCs) waiting to execute reside. The APC queues (one for user mode and one for kernel mode) are thread-specific—each thread has its own APC queues (unlike the DPC queue, which is processor- wide).

一个队列,异步过程调用在其中排队等待执行。每个线程有自己的apc队列。

 

 


Several Windows APIs, such as ReadFileEx, WriteFileEx, and QueueUserAPC, use user-mode APCs. For example, the ReadFileEx and WriteFileEx functions allow the caller to specify a completion routine to be called when the I/O operation finishes. The I/O completion is implemented by queueing an APC to the thread that issued the I/O. However, the callback to the completion routine doesn't necessarily take place when the APC is queued because user-mode APCs are delivered to a thread only when it's(thread???) in an alertable wait state. A thread can enter a wait state either by waiting for an object handle and specifying that its wait is alertable (with the Windows WaitForMultipleObjectsEx function) or by testing directly whether it has a pending APC (using SleepEx). In both cases, if a user-mode APC is pending, the kernel interrupts (alerts) the thread, transfers control to the APC routine, and resumes the thread's execution when the APC routine completes. Unlike kernel-mode APCs, which execute at APC level, usermode APCs execute at passive level.

 

 

下面的话来自于MSDN:http://msdn.microsoft.com/en-us/library/ms681951(VS.85).aspx

A thread enters an alertable state when it calls the SleepEx, SignalObjectAndWait, MsgWaitForMultipleObjectsEx, WaitForMultipleObjectsEx, or WaitForSingleObjectEx function. If the wait is satisfied before the APC is queued, the thread is no longer in an alertable wait state so the APC function will not be executed. However, the APC is still queued, so the APC function will be executed when the thread calls another alertable wait function.

  • QueueUserAPC的注意事项

来自于MSDN中QueueUserAPC的Remarks部分
To ensure successful execution of functions used by the APC, APCs should be queued only to threads in the caller's process.

Note  Queueing APCs to threads outside the caller's process is not recommended for a number of reasons. DLL rebasing can cause the addresses of functions used by the APC to be incorrect when the functions are executed outside the caller's process. Similarly, if a 64-bit process queues an APC to a 32-bit process or vice versa, addresses will be incorrect and the application will crash. Other factors can prevent successful function execution, even if the address is known.

Each thread has its own APC queue. The queuing of an APC is a request for the thread to call the APC function. The operating system issues a software interrupt to direct the thread to call the APC function.

When a user-mode APC is queued, the thread is not directed to call the APC function unless it(指thread) is in an alertable state. After the thread is in an alertable state, the thread handles all pending APCs in first in, first out (FIFO) order(以先进先出方式处理所有的pending APCs), and the wait operation returns WAIT_IO_COMPLETION.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值