My main thread creates multiple I/O worker threads. I then initiate an I/O request from my main thread by doing:
{
...
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);
...
}
At some point in one of my I/O worker threads, the request is completed and my IoRequest object's buffer is filled with valid response data.
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;
}
How can I place my main thread in an alertable wait state after calling WSASend() and make it receive the response data whenever it's ready?