将调用线程置于可警告的等待状态

时间:2012-08-06 11:52:00

标签: c++ sockets winsock2 iocp

我的主线程创建多个I / O工作线程。然后,我通过执行以下操作从主线程发起I / O请求:

{
    ...
    IoRequest *pIoRequest = new IoRequest(m_socket);
    pIoRequest->SetBuffer(vecCommandData); // vector of BYTEs
    pIoRequest->SetOperationType(OP_TYPE_SEND);
    WSASend(m_socket, pIoRequest->GetWsaBuffer(), 1, NULL, 0, pIoRequest, NULL);
    ...
}

在我的一个I / O工作线程中的某个时刻,请求已完成,我的IoRequest对象的缓冲区充满了有效的响应数据。

DWORD WINAPI WorkerThreadProc(LPVOID lpParameter)
{
    IoCompletionPort *pIocp = reinterpret_cast<IoCompletionPort*>(lpParameter);    
    ...
    while (true)
    {
        BOOL bReturn = pIocp->GetStatus(&ulCompletionKey, &dwNumberOfBytesTransferred, (LPOVERLAPPED*)&pIoRequest);
        ...
        switch (pIoRequest->GetOperationType())
        {
        ...
        case OP_TYPE_RECEIVE_DATA:
            {
                ...
                // the requested I/O operation has completed and pIoRequest's buffer now contains valid response data!
                break;
            } 
        ...
        }
    }

    return 0;
}

如何在调用WSASend()之后将主线程置于可警告的等待状态,并使其在准备就绪时接收响应数据?

1 个答案:

答案 0 :(得分:2)

在IOCP服务器中,它通常是GetQueuedCompletionStatus()。