QueueUserAPC Function

本文详细介绍了异步过程调用(APC)的工作原理及其在Windows系统中的使用方法。通过ReadFileEx/WriteFileEx等函数,可以在IO请求完成后通过APC进行异步通知。文章还介绍了如何使用QueueUserAPC函数手动触发APC。

原创   miranda-APC(异步过程调用)分析 

APC : asynchronous procdure call 异步过程调用
    Alertable IO(告警IO)提供了更有效的异步通知形式。ReadFileEx / WriteFileEx在发出IO请求的同时,
    提供一个回调函数(APC过程),当IO请求完成后,一旦线程进入可告警状态,回调函数将会执行。
    以下五个函数能够使线程进入告警状态:
    SleepEx
    WaitForSingleObjectEx
    WaitForMultipleObjectsEx
    SignalObjectAndWait
    MsgWaitForMultipleObjectsEx
    线程进入告警状态时,内核将会检查线程的APC队列,如果队列中有APC,将会按FIFO方式依次执行。
    如果队列为空,线程将会挂起等待事件对象。以后的某个时刻,一旦APC进入队列,线程将会被唤醒
    执行APC,同时等待函数返回WAIT_IO_COMPLETION。
    QueueUserAPC可以用来人为投递APC,只要目标线程处于告警状态时,APC就能够得到执行。
    使用告警IO的主要缺点是发出IO请求的线程也必须是处理结果的线程,如果一个线程退出时还有
    未完成的IO请求,那么应用程序将永远丢失IO完成通知。

 


*********************************************************************************************
QueueUserAPC Function

Adds a user-mode asynchronous procedure call (APC) object to the APC queue of the specified thread.

添加一个用户层异步过程调用对象到定义线程的APC队列中

Syntax

C++
DWORD WINAPI QueueUserAPC(
__in PAPCFUNC pfnAPC ,
__in HANDLE hThread ,
__in ULONG_PTR dwData
);

Parameters

pfnAPC [in]

A pointer to the application-supplied APC function to be called when the specified thread performs an alertable wait operation. For more information, see APCProc .

指向应用城西提供的APC函数,当定义的线程处在等待操作状态下,这个函数被调用

hThread [in]

A handle to the thread. The handle must have the THREAD_SET_CONTEXT access right. For more information, see Synchronization Object Security and Access Rights .一个线程的句柄。

dwData [in]

A single value that is passed to the APC function pointed to by the pfnAPC parameter.

Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. There are no error values defined for this function that can be retrieved by calling GetLastError .

Remarks

The APC support provided in the operating system allows an application to queue an APC object to a thread.

操作系统提供的APC支持允许某个应用程序将APC对象排队入一个线程

An APC can be queued from a 64-bit process to another 64-bit process or from a 32-bit process to another 32-bit process. You should not queue an APC from a 64-bit process to a 32-bit process or vice versa, because the address will be incorrect and the application will crash.

 

 

Each thread has its own APC queue. 每一个线程都有自己的APC队列。

 

The queuing of an APC is a request for the thread to call the APC function.

线程请求APC队列来调用APC函数

 

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 is in an alertable state.

当用户层 APC 被插入某线程的队列,此线程处于警告状态时才调用APC函数。

 

After the thread is in an alertable state, the thread handles all pending APCs in first in, first out (FIFO) order, and the wait operation returns WAIT_IO_COMPLETION

当一个线程处于警告状态,其按照先进先出的顺序处理所有APC,等待操作返回WAIT_IO_COMPLETION

 

. A thread enters an alertable state by using SleepEx , SignalObjectAndWait , WaitForSingleObjectEx , WaitForMultipleObjectsEx , or MsgWaitForMultipleObjectsEx to perform an alertable wait operation.

使用SleepEx , SignalObjectAndWait , WaitForSingleObjectEx , WaitForMultipleObjectsEx , or MsgWaitForMultipleObjectsEx 函数可以是一个进程进入警告状态。

 

If an application queues an APC before the thread begins running, the thread begins by calling the APC function. After the thread calls an APC function, it calls the APC functions for all APCs in its APC queue.

在某线程启动前,如果应用程序将APC插入队列,线程启动时调用APC函数。之后,此线程为APC队列中的所有APCs调用这个APC函数。

 

It is possible to sleep or wait for an object within the APC. If you perform an alertable wait inside an APC, it will recursively dispatch the APCs. This can cause a stack overflow.

When the thread is terminated using the ExitThread or TerminateThread function, the APCs in its APC queue are lost. The APC functions are not called.

Note that the ReadFileEx , SetWaitableTimer , and WriteFileEx functions are implemented using an APC as the completion notification callback mechanism.

To compile an application that uses this function, define _WIN32_WINNT as 0x0400 or later. For more information, see Using the Windows Headers .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值