/*
* 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 <stdio.h>
#include <stdlib.h>
#include <windows.h>
#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 finished
// 用于检测协调所有IO都操作完毕的Event
HANDLE ghEvent;
// Keep track of each individual I/O operation
OVERLAPPED gOverlapped[MAX_REQUESTS];
// Handle to the file of interest.
HANDLE ghFile;
// Need a place to put all this data
char 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);
//如果所有IO都执行完,则激活ghEvent,相当于一个集合点的作用
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
GetWindowsDirectory(szPath, sizeof(szPath));
strcat(szPath, ":\\WINHLP32.EXE");
// Open the file for overlapped reads
ghFile = CreateFileA( szPath,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL
);
if (ghFile == INVALID_HANDLE_VALUE)
{
printf("Could not open %s\n", szPath);
return -1;
}
MTVERIFY(
ghEvent = CreateEvent(
NULL, // No security
TRUE, // Manual reset - extremely important!
FALSE, // Initially set Event to non-signaled state
NULL // No name
)
);
// Queue up a few requests
for (i=0; i<MAX_REQUESTS; i++)
{
// Read some bytes every few K
QueueRequest(i, i*16384, READ_SIZE);
}
printf("QUEUED!!\n");
// Wait for all the operations to complete.
//如果所有IO都执行完则继续
for (;;)
{
DWORD rc;
rc = WaitForSingleObjectEx(ghEvent, INFINITE, TRUE );
if (rc == WAIT_OBJECT_0)
break;
MTVERIFY(rc == WAIT_IO_COMPLETION);
}
CloseHandle(ghFile);
return EXIT_SUCCESS;
}
/*
* Call ReadFileEx to start an overlapped request.
* Make sure we handle errors that are recoverable.
*/
int QueueRequest(int nIndex, DWORD dwLocation, DWORD dwAmount)
{
int i;
BOOL rc;
DWORD err;
gOverlapped[nIndex].hEvent = (HANDLE)nIndex;
gOverlapped[nIndex].Offset = dwLocation;
for (i=0; i<MAX_TRY_COUNT; i++)
{
rc = ReadFileEx(
ghFile,
gBuffers[nIndex],
dwAmount,
&gOverlapped[nIndex],
FileIOCompletionRoutine
);
// Handle success
if (rc)
{
// asynchronous i/o is still in progress
printf("Read #%d queued for overlapped I/O.\n", nIndex);
return TRUE;
}
err = GetLastError();
// Handle recoverable error
if ( err == ERROR_INVALID_USER_BUFFER ||
err == ERROR_NOT_ENOUGH_QUOTA ||
err == ERROR_NOT_ENOUGH_MEMORY )
{
Sleep(50); // Wait around and try later
continue;
}
// Give up on fatal error.
break;
}
printf("ReadFileEx failed.\n");
return -1;
}
//
// Make sure we are running under an operating
// system that supports overlapped I/O to files.
//
void CheckOsVersion()
{
OSVERSIONINFO ver;
BOOL bResult;
ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
bResult = GetVersionEx((LPOSVERSIONINFO) &ver);
if ( (!bResult) ||
(ver.dwPlatformId != VER_PLATFORM_WIN32_NT) )
{
fprintf(stderr, "IoByAPC must be run under Windows NT.\n");
exit(EXIT_FAILURE);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Event和Overlapped小例子
共21个文件
sln:5个
vcproj:5个
user:5个
需积分: 10 36 下载量 127 浏览量
2008-08-01
14:16:22
上传
评论
收藏 17KB RAR 举报
温馨提示
Event和Overlapped小例子,很简单,最后一个时侯捷的win32多线程设计的APC例子修改版,以前的不能执行
资源推荐
资源详情
资源评论
收起资源包目录
Thread.rar (21个子文件)
Thread
IOByAPC
IOByAPC
IOByAPC.vcproj 4KB
IOBYAPC.C 5KB
MTVERIFY.H 1KB
IOByAPC.vcproj.YANGZH.Administrator.user 1KB
IOByAPC.sln 886B
SimpleOverlap
SimpleOverlap.sln 904B
SimpleOverlap
main.cpp 970B
SimpleOverlap.vcproj.YANGZH.Administrator.user 1KB
SimpleOverlap.vcproj 4KB
SimpleThread
SimpleThread
main.cpp 664B
SimpleThread.vcproj 4KB
SimpleThread.vcproj.YANGZH.Administrator.user 1KB
SimpleThread.sln 901B
MltReadSglWrite
MltReadSglWrite.sln 910B
MltReadSglWrite
main.cpp 2KB
MltReadSglWrite.vcproj 4KB
MltReadSglWrite.vcproj.YANGZH.Administrator.user 1KB
overlap
overlap
main.cpp 2KB
overlap.vcproj 4KB
overlap.vcproj.YANGZH.Administrator.user 1KB
overlap.sln 886B
共 21 条
- 1
资源评论
edwardYang
- 粉丝: 7
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功