Can I use an IOCP as a waitable object?

Started by
4 comments, last by cbenoi1 19 years, 6 months ago
Hey all, I have a thread that runs in my threaded socket server, which is used to wait for IO events on the sockets. However I also need to see if the event signalling that the thread should terminate is signalled. Heres what I have at the moment:

void CSocketServer::CIOCPThread::ThreadProc()
{
HANDLE hEvents[2] = {m_hEvent,m_hIOCP};
DWORD dwRet, dwBytes;
LPOVERLAPPED pOverlapped;
CSocket* pSocket;

   do
   {
      // Wait for termination or IO event //
      dwRet = WaitForMultipleObjects(2,hEvents,FALSE,INFINITE);
      if(dwRet != WAIT_OBJECT_0+1)
         continue;

      // Get next event //
      if(!GetQueuedCompletionStatus(hEvents[1],&dwBytes,(DWORD*)&pSocket,&pOverlapped,0))
         continue;

      // Process here

   } while(dwRet != WAIT_OBJECT_0);
}



However, GetQueuedCompletionStatus always fails. I assume I can't use an IO completion port like this? Is there any better way that spinning in a loop, alternately checking for events on the IOCP and handle? If I set each one to wait for 100ms, my app should still be fairly responsive, but obviously not as responsive as I'd like. EDIT: Also, does anyone know if I can use GetQueuedCompletionStatus to see if I'm able to accept() a connection, or will I need yet another thread? So, any ideas? Cheers, Steve
Advertisement
One reason is that another thread might have closed the handle to the IOCP.

What exactly are you trying to do?

Kuphryn
I have a thread to handle all the socket stuff. At the moment, it owns a std::vector of CSocket*'s. Each SOCKET of the CSocket* is associated with the IO completion port. When the app needs to exit, it sets an event, and waits for the thread to exit (the thread handle gets a signalled state). So, in my thread, I need to wait for 2 things: IO events, and the termination event.
Apparently I can't use WaitForMultipleObjects, since it says the event is signalled, but GetQueuedCompletionStatus() returns false (Actually, I should check GetLastError(), I'll do that in a minute. EDIT, GetLastError() says "The wait operation timed out.").
The handle has definitely not been closed.

So, I need a way to be able to check for IO events and to check if the termination event is signalled.

I'm going to look into PostQueuedCompletionStatus, and try posting an IO completion packet to signal that the thread should terminate. I'll let you know how that goes...
Whee, Using this works perfectly: PostQueuedCompletionStatus(m_hIOCP,0,0xffffffff,NULL). Then I just check if pSocket is 0xffffffff. If so, I can terminate the thread.
Seems an awful round-about way of doing IOCP. IOCP's tricky stuff anyways.
daerid@gmail.com
http://pages.infinit.net/cbenoi1/iocp_udp.zip

-cb

This topic is closed to new replies.

Advertisement