为什么有需要的引用计数内存为了限制? [英] Why there need memory order limit on reference counter?

查看:148
本文介绍了为什么有需要的引用计数内存为了限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在的例子的boost ::原子 UNREF 功能:

void intrusive_ptr_release(const X * x)
{
  if (x->refcount_.fetch_sub(1, boost::memory_order_release) == 1) {
    boost::atomic_thread_fence(boost::memory_order_acquire);
    delete x;
  }
}

1:fetch_sub OP由 memory_order_release ,其中prevents preceding操作重新排序过去的点限制。但是,什么是会有这样的现象可能的场景?

1: the fetch_sub op is limited by memory_order_release, which prevents preceding operations to be reordered past the point. But what are the possible scenes that would have such phenomenon?

2:除中的 memory_order_release 在原子操作,为什么还有一个附加的 memory_order_acquire 删除前

2: in addition of memory_order_release on the atomic op, why there is an additional memory_order_acquire before the deletion?

推荐答案

对于第一个问题,prevents任何使用的(* X)来重新排序在 fetch_sub (当引用计数可能是0,因此被禁止使用)。可能的原因是CPU重新排序或编译器重新排序。第二个问题是释放只是镜;释放保护存储和获取保护负载。

For the first question, it prevents any use of (*x) to be reordered after the fetch_sub (when the reference count could be 0 and use is therefore banned). Possible causes are CPU reordering or compiler reordering. The second question is just the mirror of the release; release protects stores and acquire protects loads.

这可能看起来 refcount_fetch_sub(1,memory_order_acq_rel)工作为好,但保护只是引用计数。

It may seem that refcount_fetch_sub(1, memory_order_acq_rel) works as well, but that protects just the refcount.

这篇关于为什么有需要的引用计数内存为了限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆