4

I am using a TcpStream. I could not really find a concrete answer that indicates that I can read and write from the socket at the same time like with C. I really need to do this, since I have a struct that is constantly waiting for messages, and sometimes I have to send messages through the same stream. I already saw this post: How do I handle parallel reads and writes on a TcpStream? but try_clone does not really specify that this allows for parallel reads and writes, and I have read some posts and github issues saying that parallelism should be possible (implying that it is not). This is all with standard Rust, I am not allowed to use any framework or external crate.

Because I thought the stream was not thread safe I wrapped it in a RwLock, but now I realized that generates a deadlock, since the thread that is always waiting for a message has the lock.

Drasungor
  • 53
  • 6
  • Is https://doc.rust-lang.org/std/net/struct.TcpStream.html not reading & writing (bi-directional)? – user2864740 Dec 05 '21 at 03:37
  • 1
    As a note: the read + write buffers of TCP streams are themselves independent and thread-safe (when each used from different threads) through the entire network stack. Two threads both reading or writing, not guaranteed; one thread reading and one thread writing, OK. – user2864740 Dec 05 '21 at 03:39
  • 1
    Thank you. I talked with a friend previously about this and we only got more confused. I was aware that at least in Linux sockets are thread safe for parallel reading and writing, but some posts lead me to believe that due to OS abstruction in Rust this was not the case. I see now that was wrong, I have used now the same socket in parallel and it is working perfectly. – Drasungor Dec 07 '21 at 03:17

0 Answers0