Subj : Re: IOCP problem To : comp.programming.threads From : Richard Hollis Date : Sun Jan 30 2005 09:04 pm Thank you for taking the time to respond on this. Is there a good book you can recommend on general do's and don'ts and good practices? Thanks Richard "SenderX" wrote in message news:7OGdnW_5fNnn1GXcRVn-hw@comcast.com... > > "SenderX" wrote in message > news:YuSdnX08G6Bg22XcRVn-rg@comcast.com... > > Go ahead and try something like this: > > If found a NASTY race-condition: > > > > > if ( ! ReadFile > > ( hPipe, > > &read_buffer, > > PIPE_MSG_MAX, > > &dwBytesRead, > > lpo ) ) > > { > > DWORD err = GetLastError(); > > > > if ( err != ERROR_IO_PENDING ) > > { > > TRACE1("ReadFile dwLastErr = %d\n", err ); > > DisconnectNamedPipe( hPipe ); > > ConnectNamedPipe( hPipe, lpo ); > > state = 0; > > continue; > > } > > } > > > > ++state; > > ++state is racing with another thread that could dequeue the OVERLAPPED. You > MUST set the state "before you enqueue a completion"! > > > Something like this: > > > > if ( ! state ) > { > ++state; // BEFORE we enqueue > > if ( ! ReadFile > ( hPipe, > &read_buffer, > PIPE_MSG_MAX, > &dwBytesRead, > lpo ) ) > { > DWORD err = GetLastError(); > > if ( err != ERROR_IO_PENDING ) > { > TRACE1("ReadFile dwLastErr = %d\n", err ); > DisconnectNamedPipe( hPipe ); > ConnectNamedPipe( hPipe, lpo ); > state = 0; > continue; > } > } > } > > else if ( state == 1 ) > { > ++state; // BEFORE we enqueue > > if ( ! WriteFile > ( hPipe, > "OK", > 3, > &dwBytesWritten, > lpo ) ) > { > DWORD err = GetLastError(); > > if ( err != ERROR_IO_PENDING ) > { > TRACE1("WriteFile dwLastErr = %d\n", err ); > DisconnectNamedPipe( hPipe ); > ConnectNamedPipe( hPipe, lpo ); > state = 0; > continue; > } > } > } > > .