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=0to listen for events on two different streams? (i.e. areTokensbound to a particularEventLoopinstance?) - Can I use
Token=0to simultaneously represent both aTcpStreamand a pendingTimeout, or are they both stored in the same collection of Tokens? - Is there any harm in jumping from
0to1,000,000? (e.g. Are they being stored in a data structure that's optimized for sequential numbers?)
Thanks!
The short version:
miodoesn't actually do anything with the tokens except pass them back to you when you receive the corresponding event, so you can use whatever tokens you want as far asmiois concerned. To answer your questions individually:Sure, that's fine.
miodoesn't have a collection of tokens. If you don't need unique tokens to identify things in your application code, you're free to use the same token in different places. (I'm a bit confused by this question though, since as far as I can tell, timeouts don't use mioTokens at all)No. As I said above, mio doesn't care about the value of your tokens.