I/O 异步访问
2013-09-26 11:48
330 查看
#include <iostream> #include <string> #include <windows.h> using namespace std; HANDLE g_readEvent, g_writeEvent; int main() { g_readEvent = CreateEvent( NULL, FALSE, FALSE, NULL ); g_writeEvent = CreateEvent( NULL, FALSE, FALSE, NULL ); HANDLE hFile1 = CreateFile( TEXT("1.TXT"), GENERIC_READ | GENERIC_WRITE, 0, // must be opened with exclusive-access NULL, // no security attributes OPEN_EXISTING, // must use OPEN_EXISTING FILE_FLAG_OVERLAPPED, // overlapped I/O NULL // htemplate must be NULL for comm devices ); if( INVALID_HANDLE_VALUE == hFile1 ) { cout << " open File failed !" << endl; } else { /* read process */ BYTE bBuffer[100] = { 0 }; OVERLAPPED o = { 0 }; o.Offset = 0; o.hEvent = g_readEvent; BOOL bReadDone = ReadFile( hFile1, bBuffer, 100, NULL, &o ); /* write process */ BYTE bWriteBuffer[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; OVERLAPPED oWrite = { 0 }; oWrite.Offset = 10; oWrite.hEvent = g_writeEvent; WriteFile( hFile1, bWriteBuffer, _countof( bWriteBuffer ), NULL, &oWrite ); HANDLE h[2]; h[0] = o.hEvent; h[1] = oWrite.hEvent; while(1) { DWORD dwStatus = WaitForMultipleObjects( 2, h, FALSE, INFINITE ); switch ( dwStatus - WAIT_OBJECT_0 ) { case 0: cout << " Read complete " << endl; break; case 1: cout << " Write complete " << endl; break; default: break; } } } return 0; }
相关文章推荐
- pfring(pf_ring)编译安装
- Nagle算法
- 小评s3c2440的替代品s3c2416
- C++ 类和对象
- 【iOS开发】GDataXML使用实例
- 数据结构——查找
- 【2014校招】花旗电面
- android Fragment解析
- ※数据结构※→☆非线性结构(tree)☆============二叉树结点 链式存储结构(tree node binary list)(二十)
- CloseHandle(IntPtr handle)抛异常
- shell学习笔记之命令(四)
- hdu 4648 Magic Pen 6
- 用AD组策略------灵活禁用USB
- Windows 下编译Ffmpeg
- rails中post与comment嵌套后的REST路由规则
- gsoap 中文乱码处理
- 指针和数组相同吗?
- C# web服务器被webbench攻击及目现采用的防御措施
- powerdesigner12.5 之连接oracle时报" Could not Initialize JavaVM"
- hdu 4647 Another Graph Game