为什么IOCP的结构创建完成之后,都要在激活一下呢?
例如:
1. 产生工作线程;
2. IOCP和文件句柄绑定;
3. 激活; CreateWorkerThreads(ghCompletionPort); //
// Loop forever accepting requests new connections
// and starting reading from them.
//
for (;;)
{
struct ContextKey *pKey; clientAddressLength = sizeof(clientAddress);
newsocket = accept(listener,
(struct sockaddr *)&clientAddress,
&clientAddressLength);
if (newsocket < 0)
{
FatalError("accept() Failed");
return EXIT_FAILURE;
} // Create a context key and initialize it.
// calloc will zero the buffer
pKey = calloc(1, sizeof(struct ContextKey));
pKey->sock = newsocket;
pKey->ovOut.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
// Set the event for writing so that packets
// will not be sent to the completion port when
// a write finishes.
pKey->ovOut.hEvent = (HANDLE)((DWORD)pKey->ovOut.hEvent | 0x1); // Associate the socket with the completion port
CreateIoCompletionPort(
(HANDLE)newsocket,
ghCompletionPort,
(DWORD)pKey, // No key
0 // Use default # of threads
);×××××××××××××××××××××××××××××××××××××
// Kick off the first read
bResult = ReadFile(
(HANDLE)pKey->sock,
pKey->InBuffer,
1,
&numRead,
&pKey->ovIn
);
×××××××××××××××××××××××××××××××××××××
}
例如:
1. 产生工作线程;
2. IOCP和文件句柄绑定;
3. 激活; CreateWorkerThreads(ghCompletionPort); //
// Loop forever accepting requests new connections
// and starting reading from them.
//
for (;;)
{
struct ContextKey *pKey; clientAddressLength = sizeof(clientAddress);
newsocket = accept(listener,
(struct sockaddr *)&clientAddress,
&clientAddressLength);
if (newsocket < 0)
{
FatalError("accept() Failed");
return EXIT_FAILURE;
} // Create a context key and initialize it.
// calloc will zero the buffer
pKey = calloc(1, sizeof(struct ContextKey));
pKey->sock = newsocket;
pKey->ovOut.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
// Set the event for writing so that packets
// will not be sent to the completion port when
// a write finishes.
pKey->ovOut.hEvent = (HANDLE)((DWORD)pKey->ovOut.hEvent | 0x1); // Associate the socket with the completion port
CreateIoCompletionPort(
(HANDLE)newsocket,
ghCompletionPort,
(DWORD)pKey, // No key
0 // Use default # of threads
);×××××××××××××××××××××××××××××××××××××
// Kick off the first read
bResult = ReadFile(
(HANDLE)pKey->sock,
pKey->InBuffer,
1,
&numRead,
&pKey->ovIn
);
×××××××××××××××××××××××××××××××××××××
}
解决方案 »
- 有关VC++ +sql server 2000 ADO做MIS时 登录对话框的问题
- recv读取为零
- 请关H2协议
- 用VC7.0实现单文档多视图
- MFC的默认打印机制的问题
- WIN32 SDK中WM_PAINT的一个简单问题
- 学vc有钱途吗?
- 求救: error C2664: 'DDX_FieldCheck' : cannot convert parameter 3 from 'long' to 'int &'
- 获取了DOS窗口的句柄,如何向DOS写入数据
- 关于VC++编程
- 修改IAT,HOOK API遇到的问题.
- 如何用SendMessage或PostMessage发另一个窗口发送alt+回车
iocp解决问题的方式。
你去药店买药,始终都有一个营业员在哪里守候着等待客户的到来(iocp里这叫投递io操作)当你进入药店跟营业员交流你想要的药物最后你买到想要的药后离开。完成整个业务过程。但是如果一个药店没有营业员在哪里等候顾客而是换成顾客来了后需要自己呼叫营业员这个效率就很低了。所以你可以把iocp看做是一个药店而你需要的做的就是事先预备出来一些“ 营业员”来等待顾客的进入(就是投递io操作)随后你需要建立若干个线程来检测究竟哪些营业员完成了工作。可能某些比喻不恰当。但基本思路大致如此。
这里提到的需要关注的socket,当然也包括你后来accept的套接字哦,都需要使用 CreateIoCompletionPort去绑定