博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
win32多线程-异步过程调用(asynchronous Procedure Calls, APCs)
阅读量:6234 次
发布时间:2019-06-21

本文共 3016 字,大约阅读时间需要 10 分钟。

      使用overlapped I/O并搭配event对象-----,会产生两个基础性问题。

第一个问题是,使用WaitForMultipleObjects(),你只能等待最多达MAXIMUM_WAIT_OBJECTS个对象,在Windows NT中此值最大为64。
第二个问题是,你必须不断根据“那一个handle被激发”而计算如何反应。你必须有一个分派表格(dispatch table)和WaitForMultipleObjects()的handles数组结合起来。
      这两个问题可以靠一个所谓的异步过程调用(asynchronous Procedure Calls, APCs)解决,只要使用“Ex”版的ReadFile()和WriteFile(),你就可以调用这个机制。这两个函数允许你指定一个额外的参数,是一个callback函数,这个callback函数被称为I/O completion routine,因为系统是在某一个特别的overlapped I/O操作完成之后调用它。

异步过程调用事例:

 

/* * IoByAPC.c * * Sample code for Multithreading Applications in Win32 * This is from Chapter 6, Listing 6-3 * * Demonstrates how to use APC's (asynchronous * procedure calls) instead of signaled objects * to service multiple outstanding overlapped * operations on a file. */#define WIN32_LEAN_AND_MEAN#include 
#include
#include
#include "MtVerify.h"//// Constants//#define MAX_REQUESTS 5#define READ_SIZE 512#define MAX_TRY_COUNT 5//// Function prototypes//void CheckOsVersion();int QueueRequest(int nIndex, DWORD dwLocation, DWORD dwAmount);//// Global variables//// Need a single event object so we know when all I/O is finishedHANDLE ghEvent;// Keep track of each individual I/O operationOVERLAPPED gOverlapped[MAX_REQUESTS];// Handle to the file of interest.HANDLE ghFile;// Need a place to put all this datachar gBuffers[MAX_REQUESTS][READ_SIZE];int nCompletionCount;/* * I/O Completion routine gets called * when app is alertable (in WaitForSingleObjectEx) * and an overlapped I/O operation has completed. */VOID WINAPI FileIOCompletionRoutine( DWORD dwErrorCode, // completion code DWORD dwNumberOfBytesTransfered, // number of bytes transferred LPOVERLAPPED lpOverlapped // pointer to structure with I/O information ){ // The event handle is really the user defined data int nIndex = (int)(lpOverlapped->hEvent); printf("Read #%d returned %d. %d bytes were read.\n", nIndex, dwErrorCode, dwNumberOfBytesTransfered); if (++nCompletionCount == MAX_REQUESTS) SetEvent(ghEvent); // Cause the wait to terminate}int main(){ int i; char szPath[MAX_PATH]; CheckOsVersion(); // Need to know when to stop MTVERIFY( ghEvent = CreateEvent( NULL, // No security TRUE, // Manual reset - extremely important! FALSE, // Initially set Event to non-signaled state NULL // No name ) ); GetWindowsDirectory(szPath, sizeof(szPath)); strcat(szPath, "\\WINHLP32.EXE"); // Open the file for overlapped reads ghFile = CreateFile( szPath, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL ); if (ghFile == INVALID_HANDLE_VALUE) { printf("Could not open %s\n", szPath); return -1; } // Queue up a few requests for (i=0; i

 

 

 

转载地址:http://wzqna.baihongyu.com/

你可能感兴趣的文章
ELK搭建以及使用大全
查看>>
本地HOSTS测试管理工具
查看>>
httpwatch使用技巧
查看>>
视图的with check option解释
查看>>
我的友情链接
查看>>
安装nginx+tomcat
查看>>
Android配置环境与引入第三方jar包
查看>>
我的友情链接
查看>>
iOS中UIWebView与其中网页的javascript的交互
查看>>
For语句实现批量创建AD用户
查看>>
MAC与LINUX之间的文件通信
查看>>
【MyBatis框架】SqlMapConfigl配置文件之常用的setting设置
查看>>
条件编译
查看>>
京东金融大数据竞赛猪脸识别(1)-从视频提取图像
查看>>
CentOS6.x/CentOS7.x一键安装mysql5.6/5.7并定制数据目录
查看>>
iOS消息转发机制
查看>>
css3样式的经典实现
查看>>
初次来到51CTO
查看>>
如何成为一名数据科学家?
查看>>
linux nc命令常用用法
查看>>