Search code examples
c++multithreadingsocketsiocp

Is calling WSASend() and WSARecv() from two threads safe when using IOCP?


Based on this article, calling WSASend() or WSARecv() from multiple threads is not safe. However, is it safe to call WSASend() and WSARecv() at the same time from two threads?


Solution

  • It is always safe to read from a socket in one thread while simultaneously writing to the same socket in another thread. Separate kernel buffers are involved. Regardless of whether you are using blocking, non-blocking, overlapped, or IOCP I/O logic.

    What is not safe is simultaneously reading from the same socket in multiple threads, or simultaneously writing to the same socket in multiple threads. That requires synchronization between the threads so their transmitted/received data do not overlap each other.