std :: shared_ptr和std :: experimental :: atomic_shared_ptr有什么区别? [英] What is the difference between std::shared_ptr and std::experimental::atomic_shared_ptr?

查看:170
本文介绍了std :: shared_ptr和std :: experimental :: atomic_shared_ptr有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了以下文章: Antony Williams ,据我了解,除了std::experimental::atomic_shared_ptrstd::shared_ptr中的原子共享计数之外,指向共享对象的实际指针还是原子的吗?

I read the following article by Antony Williams and as I understood in addition to the atomic shared count in std::shared_ptr in std::experimental::atomic_shared_ptr the actual pointer to the shared object is also atomic?

但是,当我阅读有关安东尼的书中关于 似乎也适用于std::shared_ptr,因为std::atomic_loadstd::atomic_compare_exchnage_weak之类的函数适用于std::shared_ptr的实例.

But when I read about reference counted version of lock_free_stack described in Antony's book about C++ Concurrency it seems for me that the same aplies also for std::shared_ptr, because functions like std::atomic_load, std::atomic_compare_exchnage_weak are applied to the instances of std::shared_ptr.

template <class T>
class lock_free_stack
{
public:
  void push(const T& data)
  {
    const std::shared_ptr<node> new_node = std::make_shared<node>(data);
    new_node->next = std::atomic_load(&head_);
    while (!std::atomic_compare_exchange_weak(&head_, &new_node->next, new_node));
  }

  std::shared_ptr<T> pop()
  {
    std::shared_ptr<node> old_head = std::atomic_load(&head_);
    while(old_head &&
          !std::atomic_compare_exchange_weak(&head_, &old_head, old_head->next));
    return old_head ? old_head->data : std::shared_ptr<T>();
  }

private:
  struct node
  {
    std::shared_ptr<T> data;
    std::shared_ptr<node> next;

    node(const T& data_) : data(std::make_shared<T>(data_)) {}
  };

private:
  std::shared_ptr<node> head_;
};

这两种类型的智能指针之间的确切区别是什么?如果std::shared_ptr实例中的指针不是原子的,为什么可以实现上述无锁堆栈?

What is the exact difference between this two types of smart pointers, and if pointer in std::shared_ptr instance is not atomic, why it is possible the above lock free stack implementation?

推荐答案

shared_ptr中的原子事物"不是共享指针本身,而是它所指向的控制块.这意味着只要您不跨多个线程对shared_ptr进行突变,就可以了.请注意,复制 shared_ptr仅会更改控制块,而不会更改shared_ptr本身.

The atomic "thing" in shared_ptr is not the shared pointer itself, but the control block it points to. meaning that as long as you don't mutate the shared_ptr across multiple threads, you are ok. do note that copying a shared_ptr only mutates the control block, and not the shared_ptr itself.

std::shared_ptr<int> ptr = std::make_shared<int>(4);
for (auto i =0;i<10;i++){
   std::thread([ptr]{ auto copy = ptr; }).detach(); //ok, only mutates the control block 
}

更改共享指针本身(例如,从多个线程为其分配不同的值)是一种数据竞争,例如:

Mutating the shared pointer itself, such as assigning it different values from multiple threads, is a data race, for example:

std::shared_ptr<int> ptr = std::make_shared<int>(4);
std::thread threadA([&ptr]{
   ptr = std::make_shared<int>(10);
});
std::thread threadB([&ptr]{
   ptr = std::make_shared<int>(20);
});    

在这里,我们要使控制块(它可以指向多个线程中的不同值)对共享块本身(也可以)进行控制.这不行.

Here, we are mutating the control block (which is ok) but also the shared pointer itself, by making it point to a different values from multiple threads. This is not ok.

该问题的解决方案是用锁包装shared_ptr,但是该解决方案在某些争用中不是那么可伸缩,并且在某种意义上失去了标准共享指针的自动感觉.

A solution to that problem is to wrap the shared_ptr with a lock, but this solution is not so scalable under some contention, and in a sense, loses the automatic feeling of the standard shared pointer.

另一种解决方案是使用您引用的标准函数,例如std::atomic_compare_exchange_weak.这使同步共享指针的工作成为手动操作,这是我们不喜欢的.

Another solution is to use the standard functions you quoted, such as std::atomic_compare_exchange_weak. This makes the work of synchronizing shared pointers a manual one, which we don't like.

这是原子共享指针起作用的地方.您可以从多个线程中更改共享指针,而不必担心数据争用和不使用任何锁.独立功能将是成员功能,对用户而言,它们的使用将更加自然.这种指针对于无锁数据结构非常有用.

This is where atomic shared pointer comes to play. You can mutate the shared pointer from multiple threads without fearing a data race and without using any locks. The standalone functions will be members ones, and their use will be much more natural for the user. This kind of pointer is extremely useful for lock-free data structures.

这篇关于std :: shared_ptr和std :: experimental :: atomic_shared_ptr有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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