The mio
library for asynchronous I/O relies on the developer to provide instances of the Token
type in order to correlate events that have happened back to the source, e.g. a particular TcpStream
or Handler::Timeout
.
As you can see from the implementation, Token
is just a wrapper type around a usize
. It's tempting to simply increment a counter each time a Token
is needed, but it will eventually overflow.
What rules should I keep in mind as I go to generate Tokens
to pass to the EventLoop
? Some specific questions:
- If I have two threads who each have their own
EventLoop
, can they both useToken=0
to listen for events on two different streams? (i.e. areTokens
bound to a particularEventLoop
instance?) - Can I use
Token=0
to simultaneously represent both aTcpStream
and a pendingTimeout
, or are they both stored in the same collection of Tokens? - Is there any harm in jumping from
0
to1,000,000
? (e.g. Are they being stored in a data structure that's optimized for sequential numbers?)
Thanks!