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 pin(&self) -> PinGuard<'_, T>
pub fn pin(&self) -> PinGuard<'_, T>
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当被钉住时,线程被认为在特定版本“活跃“,垃圾回收器不会回收该版本的数据。