当释放仅发生在互斥锁的锁定区域时,我需要获取/释放吗?

时间:2018-10-18 21:34:17

标签: rust mutex atomic

我正在编写具有不变数据的线程安全可扩展数组/向量。

要接收元素的长度,我加载一个原子整数。互斥量可以保护更多的空间。在预订时,我必须更新长度。由于reserve不是同时调用的,我是否需要使用发布语义存储值?是否有可能以指定的其他顺序执行锁定区域中的指令?

fn len(&self) -> usize {
    // Do we need Acquire or is Relaxed enough?
    self.len.load(atomic::Ordering::Acquire)
}

fn reserve(&self, additional: usize) {
    let _guard = self.mutex.lock();
    // this call happens while the mutex is locked. Is Relaxed fine or do we 
    // need Acquire?
    let current_len = self.len.load(atomic::Ordering::Relaxed);

    // ... do the reserving stuff ...

    // this call happens while the mutex is locked. Do we need Release semantic
    // or is Relaxed enough?
    self.len.store(current_len + additional, atomic::Ordering::Release);
}

我故意没有添加代码来获取数据,因为这将导致此处不相关的讨论。

0 个答案:

没有答案