在WINDOWS里面有一个重叠i/o模型得例子,在他的HandleIo函数里面有这样一段代码,
SOCKET_OBJ *sock=NULL,
*clientobj=NULL;     // New client object for accepted connections
BUFFER_OBJ *recvobj=NULL,       // Used to post new receives on accepted connections
*sendobj=NULL;       // Used to post sends for data received
DWORD       bytes,
flags;
BOOL        bFreeSocketObj;
int         error,
rc; // Extract the SOCKET_OBJ from the BUFFER_OBJ for easy reference
sock = buf->Socket;
error = NO_ERROR; bFreeSocketObj = FALSE; InterlockedDecrement(&sock->OutstandingOps); // Get the results of the overlapped operation that completed
rc = WSAGetOverlappedResult(
sock->s,
&buf->ol,
&bytes,
FALSE,
&flags
);
if (rc == FALSE)
{
error = WSAGetLastError(); fprintf(stderr, "HandleIo: WSAGetOverlappedResult failed: %d\n", error);
if (gProtocol == IPPROTO_TCP)
{
// An error occured on a TCP socket, so remove this I/O and if no
//    more I/O is outstanding, free the socket object. Otherwise,
//    wait for the remaining I/O on this socket to complete as well.
RemoveBufferFromThread(sock, buf);
FreeBufferObj(buf); if (InterlockedDecrement(&sock->OutstandingOps) == 0)
{
printf("Freeing socket obj in GetOverlapepdResult\n");
FreeSocketObj(sock);
} return;
}
}

它在一开始调用了InterlockedDecrement,将sock->OutstandingOps减少一,紧接着在WSAGetLastError有错的情况下有调用了一次if (InterlockedDecrement(&sock->OutstandingOps) == 0),我觉得这个地方应该直接判断if (sock->OutstandingOps == 0),为什么还要减一呢,谢谢!