Jump to content
  • Advertisement
Sign in to follow this  
Evil Steve

Can I use an IOCP as a waitable object?

This topic is 5454 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
One reason is that another thread might have closed the handle to the IOCP.

What exactly are you trying to do?

Kuphryn

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!