Subj : Re: IOCP problem To : comp.programming.threads From : SenderX Date : Wed Jan 26 2005 06:04 pm "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; } } } .