In Linux, if I create two threads, one for TCP send data and one for TCP receive data for the same nonblocking socket, is it thread safe?
Or should I synchronise using a mutex?
It's absolutely fine. There's already a mutex or equivalent synchronization inside the read/write/send/recv
etc. library functions.
These functions for working with sockets are all from the POSIX.1-2008 standard.
If you check man 7 pthreads
, it says that
POSIX.1-2001 and POSIX.1-2008 require that all functions specified in the standard shall be thread-safe, except for ...
(... there follows a list of functions which you can confirm you don't care about).