IOCP

IOCP是管理线程池的机制,而这些创建这些线程的唯一目的就是处理I/O请求.主要应用于处理大量的并行的异步I/O请求.
 CreateIoCompletionPort 将一个IOCP与一个或多个FileHandle相关联,
当在已关联的FileHandle上有异步I/O操作时,一个I/O完成信息包被加入端口的队列.

HANDLE WINAPI CreateIoCompletionPort( __in HANDLE FileHandle, __in HANDLE ExistingCompletionPort, __in ULONG_PTR CompletionKey, __in DWORD NumberOfConcurrentThreads );

FileHandle
      INVALID_HANDLE_VALUE:创建完成端口;
                     ExistingCompletionPort必须为NULL ,CompletionKey和NumberOfConcurrentThreads 将被忽略.
ExistingCompletionPort 
      NULL:创建完成端口 
      否则关联FileHandle
CompletionKey 
      完成键:这儿放什么,GetQueuedCompletionStatus取的就是什么
NumberOfConcurrentThreads 
      允许创建的最大线程数,ExistingCompletionPort 为空时将被忽略;
      0:允许创建的最大线程数为系统中的CPU数.
创建一个线程,调用GetQueuedCompletionStatus等待一个完成信息包被加入到端口的队列(队列是后进先出的,last-in-first-out (LIFO)).
当线程调用GetQueuedCompletionStatus时,将一直与这个完成端口关联,直到线程退出、指定其他的完成端口或释放完成端口。
一个线程最多只能关联一个完成端口。
BOOL WINAPI GetQueuedCompletionStatus(
  __in          HANDLE CompletionPort,
  __out         LPDWORD lpNumberOfBytes,
  __out         PULONG_PTR lpCompletionKey,
  __out         LPOVERLAPPED* lpOverlapped,
  __in          DWORD dwMilliseconds
);
CompletionPort 
    完成端口
lpNumberOfBytes 
    传送的字节数
lpCompletionKey 
    完成键值:取出CreateIoCompletionPort放的值
lpOverlapped 
dwMilliseconds 
    等待时长,单位MS。超时返回FALSE,*lpOverlapped设为NULL;
    INFINITE:不会超时
    0:队列为空立即超时
PostQueuedCompletionStatus 添加用户自定义的完成信息包到完成端口队列。
BOOL WINAPI PostQueuedCompletionStatus(
  __in          HANDLE CompletionPort,
  __in          DWORD dwNumberOfBytesTransferred,
  __in          ULONG_PTR dwCompletionKey,
  __in          LPOVERLAPPED lpOverlapped
);
CompletionPort 
    完成端口
dwNumberOfBytesTransferred 
    传送的字节数
dwCompletionKey 
   完成键值(自定义)
lpOverlapped 
附:OVERLAPPED Structure
 
Contains information used in asynchronous (or overlapped) input and output (I/O).
 
typedef struct _OVERLAPPED {
ULONG_PTR Internal;
ULONG_PTR InternalHigh;
union {
struct {
DWORD Offset;
DWORD OffsetHigh;
};
PVOID Pointer;
};
HANDLE hEvent; } OVERLAPPED,
*LPOVERLAPPED;
Members
Internal

Reserved for operating system use. This member, which specifies a system-dependent status, is valid when the GetOverlappedResult function returns without setting the extended error information to ERROR_IO_PENDING.

InternalHigh

Reserved for operating system use. This member, which specifies the length of the data transferred, is valid when the GetOverlappedResult function returns TRUE.

Offset

The file position at which to start the transfer. The file position is a byte offset from the start of the file. The calling process must set this member before calling the ReadFile or WriteFile function.

This member is used only when the device is a file. Otherwise, this member must be zero.

OffsetHigh

The high-order word of the file position at which to start the transfer.

This member is used only when the device is a file. Otherwise, this member must be zero.

Pointer

Reserved for system use; do not use.

hEvent

A handle to the event that will be set to the signaled state when the operation has been completed. The calling process must set this member either to zero or a valid event handle before calling any overlapped functions. To create an event object, use the CreateEvent function. This function returns a handle that can be used to synchronize simultaneous I/O requests for a device.

Functions such as ReadFile and WriteFile set this handle to the nonsignaled state before they begin an I/O operation. When the operation has completed, the handle is set to the signaled state.

Functions such as GetOverlappedResult and the wait functions reset auto-reset events to the nonsignaled state. Therefore, if you use an auto-reset event, your application can hang if you wait for the operation to complete then call GetOverlappedResult.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值