As Windows doesn't provide UNIX domain sockets, I am using a a local TCP connection to simulate the behaviour. Now, POSIX guarantees that if several threads are writing to the UNIX domain socket in parallel, chunks up to PIPE_BUF will be handled atomically - i.e. no interleaving will happen. Is there are similar guarantee on local TCP winsock or do I have to synchronise the writers using critical section?
Its not explicitly guaranteed. Lock. Uncontended locks are cheap unless you are in an extremely tight loop. If you really don't want to do this, use overlapped i/o.
If you have several threads writing to the same socket then each write call will be atomic but each interleaved with respect to other write calls that are occurring on different threads;
So, if you have thread 1 writing a string of A's in a single write and thread 2 writing a series of B's with one write and a series of C's with another then you might get ABC, or BAC or BCA but you wont get a broken run of A's with some B's in the middle...
If you require that the two writes that are being issued by thread 2 are not interleaved with the write that is issued by thread 1 (that is ABC and BCA are fine but BAC is not) then you should either use a single call to WSASend()
in thread 2 with the two buffers in an array of WSABUF structs (scatter/gather writing) or you need to lock around the write calls so that thread 1 can't interrupt.