Can TcpStream be read from a different thread?
q2p opened this issue · comments
Some time ago I remember seeing a notice along the lines of "Streams registered to a Poll on one thread can only be read on the same thread," or "Streams which Events were received on one thread must be read on the same thread." I don't remember the exact wording or the version of mio, but I can't find such a line in current docs.
Does that mean that it is now possible to create connections, register for events, poll events and read streams on different threads?
For example:
Thread 1: creates a TcpStream
Thread 2: creates a Poll, registers and and polls events
Thread 3: reads from and writes to the TcpStream after a readiness event is received.
Would that configuration work or am I missing something?
Some time ago I remember seeing a notice along the lines of "Streams registered to a Poll on one thread can only be read on the same thread," or "Streams which Events were received on one thread must be read on the same thread." I don't remember the exact wording or the version of mio, but I can't find such a line in current docs. Does that mean that it is now possible to create connections, register for events, poll events and read streams on different threads?
I don't know where you saw this, but both statements are inaccurate.
For example: Thread 1: creates a TcpStream Thread 2: creates a Poll, registers and and polls events Thread 3: reads from and writes to the TcpStream after a readiness event is received.
Would that configuration work or am I missing something?
TcpStream
, like all other types exposed by Mio, are usable from any thread. Some methods need mutable, e.g. Poll::poll
, but then we use &mut self
instead of &self
. If the type is Send
and the method takes an immutable reference (i.e. &self
) it's safe to use across threads.
Btw the above goes for any Rust code, otherwise it would be unsound.