Subj : Re: IOCP problem To : comp.programming.threads From : SenderX Date : Wed Jan 26 2005 05:36 pm > unsigned __stdcall ThreadPoolProc( void * lpParam) > { > ULONG nSocket; > DWORD nBytesToBeRead, dwLastErr; > OVERLAPPED * lpo = NULL; > BOOL b; > BYTE read_buffer[PIPE_MSG_MAX]; > > > for(;;) > { > BOOL bIORet = GetQueuedCompletionStatus( > g_hCompletionPort, > &nBytesToBeRead, > &nSocket, > &lpo, > INFINITE); > > dwLastErr = GetLastError(); > > HANDLE hPipe = m_hPipes[nSocket]; > DWORD dwActivePipe = nSocket; > int & state = m_states[dwActivePipe]; > > if(!bIORet && dwLastErr != WAIT_TIMEOUT) > { An overlapped pointer can still be dequeued even though bIORet == FALSE. > break; > } > > if(bIORet && NULL != lpo) > { Again, an overlapped pointer can still be dequeued even though bIORet == FALSE. > DWORD dwBytesRead = nBytesToBeRead; > DWORD dwBytesWritten; > > if( state == 0 ) > { > b = ReadFile( hPipe, > &read_buffer, > PIPE_MSG_MAX, > &dwBytesRead, > lpo > ); > > dwLastErr = GetLastError(); > > if(!b) > { > if( dwLastErr == ERROR_IO_PENDING ) > { ERROR_IO_PENDING means that the completion will come in with GQCS(). You will deadlock right here: > DWORD event = WaitForSingleObject(lpo->hEvent, INFINITE); > > b = GetOverlappedResult(hPipe, > lpo, > &dwBytesRead, > FALSE); > > if(!b) > { > dwLastErr = GetLastError(); > } GetOverlappedResult? You are mixing two different completion detection methods together. > } > else > { > TRACE1("ReadFile dwLastErr = %d\n", dwLastErr); > } > } > > if(!b) > { > DisconnectNamedPipe( hPipe ); > ConnectNamedPipe( hPipe, lpo ); > state = 0; > continue; > } > > // do something with message > > state++; > } > else if(state == 1) > { > b = WriteFile(hPipe, > "OK", > 3, > &dwBytesWritten, > lpo); > > dwLastErr = GetLastError(); > > if(!b) > { > if( dwLastErr == ERROR_IO_PENDING ) > { Again, ERROR_IO_PENDING means that the completion will come in with GQCS(). You will deadlock right here: > DWORD event = WaitForSingleObject(lpo->hEvent, INFINITE ); > > if( event == WAIT_OBJECT_0 ) > { > b = GetOverlappedResult(hPipe, > lpo, > &dwBytesWritten, > TRUE); > } > } > else > { > TRACE1("WriteFile dwLastErr = %d\n", dwLastErr); > } > } > > if(!b) > { > DisconnectNamedPipe( hPipe ); > ConnectNamedPipe( hPipe, lpo ); > state = 0; > continue; > } > > state++; > } > else if(state == 2) > { > DisconnectNamedPipe( hPipe ); > ConnectNamedPipe( hPipe, lpo ); > state = 0; > } > } > } > > _endthreadex(0); > > return 0; > } .