生成 mio 的 Token 时需要注意哪些限制?
What constraints should I be aware of when generating mio's Tokens?
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
.
从 the implementation 可以看出,Token
只是 usize
的包装类型。每次需要 Token
时简单地增加一个计数器很诱人,但它最终会溢出。
在生成 Tokens
传递给 EventLoop
时,我应该牢记哪些规则?一些具体问题:
- 如果我有两个线程,每个线程都有自己的
EventLoop
,它们能否都使用 Token=0
来侦听两个不同流上的事件? (即 Tokens
是否绑定到特定的 EventLoop
实例?)
- 我可以使用
Token=0
同时表示 TcpStream
和待处理的 Timeout
,还是它们都存储在同一个代币集合中?
- 从
0
跳到1,000,000
有什么坏处吗? (例如,它们是否存储在针对序列号优化的数据结构中?)
谢谢!
简短版本:mio
除了在您收到相应事件时将它们传回给您之外,实际上不会对令牌做任何事情,因此您可以使用任何您想要的令牌 mio
关注。单独回答您的问题:
If I have two threads who each have their own EventLoop, can they both use Token=0 to listen for events on two different streams? (i.e. are Tokens bound to a particular EventLoop instance?)
当然可以。
Can I use Token=0 to simultaneously represent both a TcpStream and a pending Timeout, or are they both stored in the same collection of Tokens?
mio
没有令牌集合。如果您不需要唯一的令牌来标识应用程序代码中的内容,您可以在不同的地方自由使用相同的令牌。 (虽然我对这个问题有点困惑,因为据我所知,超时根本不使用 mio Token
s)
Is there any harm in jumping from 0 to 1,000,000? (e.g. Are they being stored in a data structure that's optimized for sequential numbers?)
没有。正如我上面所说,mio 不关心你的代币价值。
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
.
从 the implementation 可以看出,Token
只是 usize
的包装类型。每次需要 Token
时简单地增加一个计数器很诱人,但它最终会溢出。
在生成 Tokens
传递给 EventLoop
时,我应该牢记哪些规则?一些具体问题:
- 如果我有两个线程,每个线程都有自己的
EventLoop
,它们能否都使用Token=0
来侦听两个不同流上的事件? (即Tokens
是否绑定到特定的EventLoop
实例?) - 我可以使用
Token=0
同时表示TcpStream
和待处理的Timeout
,还是它们都存储在同一个代币集合中? - 从
0
跳到1,000,000
有什么坏处吗? (例如,它们是否存储在针对序列号优化的数据结构中?)
谢谢!
简短版本:mio
除了在您收到相应事件时将它们传回给您之外,实际上不会对令牌做任何事情,因此您可以使用任何您想要的令牌 mio
关注。单独回答您的问题:
If I have two threads who each have their own EventLoop, can they both use Token=0 to listen for events on two different streams? (i.e. are Tokens bound to a particular EventLoop instance?)
当然可以。
Can I use Token=0 to simultaneously represent both a TcpStream and a pending Timeout, or are they both stored in the same collection of Tokens?
mio
没有令牌集合。如果您不需要唯一的令牌来标识应用程序代码中的内容,您可以在不同的地方自由使用相同的令牌。 (虽然我对这个问题有点困惑,因为据我所知,超时根本不使用 mio Token
s)
Is there any harm in jumping from 0 to 1,000,000? (e.g. Are they being stored in a data structure that's optimized for sequential numbers?)
没有。正如我上面所说,mio 不关心你的代币价值。