pub struct LocalReader<T: 'static> { /* private fields */ }Expand description
A reader thread’s local version state.
Each reader thread should create exactly one LocalReader via SwmrCell::local().
It is !Sync (due to Cell) and must be stored per-thread.
The LocalReader is used to:
- Pin the thread to the current version via
pin(). - Obtain a
PinGuardthat protects access to values and can be dereferenced.
Thread Safety: LocalReader is not Sync and must be used by only one thread.
读者线程的本地版本状态。
每个读者线程应该通过 SwmrCell::local() 创建恰好一个 LocalReader。
它是 !Sync 的(因为 Cell),必须在每个线程中存储。
LocalReader 用于:
- 通过
pin()将线程钉住到当前版本。 - 获取保护对值访问的
PinGuard,可以解引用来读取值。 线程安全性:LocalReader不是Sync的,必须仅由一个线程使用。
Implementations§
Source§impl<T: 'static> LocalReader<T>
impl<T: 'static> LocalReader<T>
Sourcepub fn is_pinned(&self) -> bool
pub fn is_pinned(&self) -> bool
Pin this thread to the current version.
Returns a PinGuard that keeps the thread pinned for its lifetime.
The guard can be dereferenced to access the current value.
Reentrancy: This method is reentrant. Multiple calls can be nested, and the thread
remains pinned until all returned guards are dropped. You can also clone a guard to create
additional references: let guard2 = guard1.clone();
Example:
let local = cell.local();
let guard1 = local.pin();
let value = *guard1; // Dereference to read
let guard2 = local.pin(); // Reentrant call
let guard3 = guard1.clone(); // Clone for nested scope
// Thread remains pinned until all three guards are droppedWhile pinned, the thread is considered “active” at a particular version, and the garbage collector will not reclaim data from that version.
将此线程钉住到当前版本。
返回一个 PinGuard,在其生命周期内保持线程被钉住。
可以解引用该守卫来访问当前值。
可重入性:此方法是可重入的。多个调用可以嵌套,线程在所有返回的守卫被 drop 之前保持被钉住。
你也可以克隆一个守卫来创建额外的引用:let guard2 = guard1.clone();
示例:
let local = cell.local();
let guard1 = local.pin();
let value = *guard1; // 解引用来读取
let guard2 = local.pin(); // 可重入调用
let guard3 = guard1.clone(); // 克隆用于嵌套作用域
// 线程保持被钉住直到所有三个守卫被 drop当被钉住时,线程被认为在特定版本“活跃“,垃圾回收器不会回收该版本的数据。 Check if this reader is currently pinned.
检查此读者当前是否被 pin。
Sourcepub fn version(&self) -> usize
pub fn version(&self) -> usize
Get the current global version.
Note: This returns the global version, not the pinned version.
To get the pinned version, use PinGuard::version().
获取当前全局版本。
注意:这返回全局版本,而不是 pin 的版本。
要获取 pin 的版本,请使用 PinGuard::version()。
pub fn pin(&self) -> PinGuard<'_, T>
Create a new SwmrReader from this LocalReader.
SwmrReader is Sync + Clone and acts as a factory for LocalReaders.
This is equivalent to calling swmr_cell.reader(), but using the LocalReader’s reference to the shared state.
从此 LocalReader 创建一个新的 SwmrReader。
SwmrReader 是 Sync + Clone 的,充当 LocalReader 的工厂。
这相当于调用 swmr_cell.reader(),但使用 LocalReader 对共享状态的引用。
Sourcepub fn into_swmr(self) -> SwmrReader<T>
pub fn into_swmr(self) -> SwmrReader<T>
Convert this LocalReader into a SwmrReader.
This consumes the LocalReader and returns a SwmrReader
that can be sent to another thread to create new LocalReaders.
将此 LocalReader 转换为 SwmrReader。
这会消耗 LocalReader 并返回一个 SwmrReader,
该 SwmrReader 可以发送到另一个线程以创建新 LocalReader。