大网数据广告活动
磁盘与文件系统

Alertable I/O

· 52CE 我爱测

Alertable I/O is the method by which application threads process asynchronous I/O requests only when they are in an alertable state.

Alertable I/O is the method by which application threads process asynchronous I/O requests only when they are in an alertable state.

To understand when a thread is in an alertable state, consider the following scenario:

  1. A thread initiates an asynchronous read request by calling ReadFileEx with a pointer to a callback function.
  2. The thread initiates an asynchronous write request by calling WriteFileEx with a pointer to a callback function.
  3. The thread calls a function that fetches a row of data from a remote database server.

In this scenario, the calls to ReadFileEx and WriteFileEx will most likely return before the function call in step 3. When they do, the kernel places the pointers to the callback functions on the thread's Asynchronous Procedure Call (APC) queue. The kernel maintains this queue specifically to hold returned I/O request data until it can be processed by the corresponding thread.

When the row fetch is complete and the thread returns from the function, its highest priority is to process the returned I/O requests on the queue by calling the callback functions. To do this, it must enter an alertable state. A thread can only do this by calling one of the following functions with the appropriate flags:

When the thread enters an alertable state, the following events occur:

  1. The kernel checks the thread's APC queue. If the queue contains callback function pointers, the kernel removes the pointer from the queue and sends it to the thread.
  2. The thread executes the callback function.
  3. Steps 1 and 2 are repeated for each pointer remaining in the queue.
  4. When the queue is empty, the thread returns from the function that placed it in an alertable state.

In this scenario, once the thread enters an alertable state it will call the callback functions sent to ReadFileEx and WriteFileEx, then return from the function that placed it in an alertable state.

If a thread enters an alertable state while its APC queue is empty, the thread's execution will be suspended by the kernel until one of the following occurs:

  • The kernel object that is being waited on becomes signaled.
  • A callback function pointer is placed in the APC queue.

A thread that uses alertable I/O processes asynchronous I/O requests more efficiently than when they simply wait on the event flag in the OVERLAPPED structure to be set, and the alertable I/O mechanism is less complicated than I/O completion ports to use. However, alertable I/O returns the result of the I/O request only to the thread that initiated it. I/O completion ports do not have this limitation.

Related topics

Asynchronous Procedure Calls

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 05/31/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。