C socket API is thread safe? [duplicate] C socket API is thread safe? [duplicate] multithreading multithreading

C socket API is thread safe? [duplicate]


Sockets are not part of C++ Standard so it depends on implementation. Generally they are not thread safe since send is not an atomic operation. Check this discussion for additional information.

EDIT: OS could have or couldn't have internal lock for protecting internal structures. It depends on implementation. So you should not count on it.


I find multiple socket close() file descriptor calls extremely dangerous in concurrent environment.

Usually multiple calls are ignored, but in case other thread opens another file descriptor, quite often it gets previous file descriptor, and nightmare starts.


Sending data via a socket is not a atomic transaction - any non-atomic transaction will require a lock/synchronisation. This is independent of the platform.