为什么 I/O 完成端口中有 CompletionKey?
MSDN 关于 CreateIoCompletionPort 函数中 CompletionKey 的评论:
使用 CompletionKey 参数来帮助您的应用程序跟踪 I/O 操作已完成。该值不被使用 CreateIoCompletionPort用于功能控制;相反,它是附加的 到当时 FileHandle 参数中指定的文件句柄 与 I/O 完成端口关联。这个完成键应该 每个文件句柄都是唯一的,并且它伴随着文件句柄 整个内部完成排队过程。它返回于 当完成数据包时调用 GetQueuedCompletionStatus 函数 到达。 CompletionKey 参数也由 PostQueuedCompletionStatus 函数用于对您自己的特殊用途进行排队 完成数据包。
上述言论给我留下了一个疑问。 既然我们可以将用户上下文与 扩展重叠结构像这样:
typedef struct s_overlappedplus
{
OVERLAPPED ol;
int op_code;
/*we can alternatively put user context over here instead of CompletionKey*/
LPVOID user_context;
} t_overlappedplus;
完成后通过 CONTAINING_RECORD 宏检索?
酷,我只确信 CompletionKey 是每个句柄上下文,而扩展重叠结构是每个 I/O 上下文。但这样的设计背后的理念是什么?在什么情况下需要使用CompletionKey来代替用户上下文的扩展重叠结构?
Remark from MSDN about CompletionKey in CreateIoCompletionPort function:
Use the CompletionKey parameter to help your application track which
I/O operations have completed. This value is not used by
CreateIoCompletionPort for functional control; rather, it is attached
to the file handle specified in the FileHandle parameter at the time
of association with an I/O completion port. This completion key should
be unique for each file handle, and it accompanies the file handle
throughout the internal completion queuing process. It is returned in
the GetQueuedCompletionStatus function call when a completion packet
arrives. The CompletionKey parameter is also used by the
PostQueuedCompletionStatus function to queue your own special-purpose
completion packets.
The above remarks leave me a question. Why use the CompletionKey given that we can associate user context with the file handle in an extended overlapped structure like this:
typedef struct s_overlappedplus
{
OVERLAPPED ol;
int op_code;
/*we can alternatively put user context over here instead of CompletionKey*/
LPVOID user_context;
} t_overlappedplus;
and retreive through CONTAINING_RECORD macro after completion?
Cool, I'm only convinced that CompletionKey is per-handle context while the extended overlapped structure is per-I/O one. But what's the philosophy behind such design and in what circumstance can it be necessary to use CompletionKey instead of an extended overlapped structure in term of user context?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论