带有引用计数

时间:2017-05-25 17:44:01

标签: c++ c++11 concurrency lock-free

从我上一个问题的答案: Pointers in c++ after delete

它变得清晰,使用指针的值,指向"删除"内存(特别是复制它们)会导致c ++ 11中的undef行为,以及c ++ 14中实现定义的行为。

Antomy Williams在他的着作" C ++并发行动"建议使用引用计数的下一个无锁堆栈:

#include <atomic>
#include <memory>

template<typename T>
class lock_free_stack
{
private:
    struct node;
    struct counted_node_ptr
    {
        int external_count;
        node* ptr;
    };
    struct node
    {
        std::shared_ptr<T> data;
        std::atomic<int> internal_count;
        counted_node_ptr next;
        node(T const& data_):
            data(std::make_shared<T>(data_)),
            internal_count(0)
        {}
    };
    std::atomic<counted_node_ptr> head;
    void increase_head_count(counted_node_ptr& old_counter)
    {
        counted_node_ptr new_counter;
        do
        {
            new_counter=old_counter;
            ++new_counter.external_count;
        }
        while(!head.compare_exchange_strong(
                  old_counter,new_counter,
                  std::memory_order_acquire,
                  std::memory_order_relaxed));
        old_counter.external_count=new_counter.external_count;
    }
public:
    ~lock_free_stack()
    {
        while(pop());
    }
    void push(T const& data)
    {
        counted_node_ptr new_node;
        new_node.ptr=new node(data);
        new_node.external_count=1;
        new_node.ptr->next=head.load(std::memory_order_relaxed)
            while(!head.compare_exchange_weak(
                      new_node.ptr->next,new_node,
                      std::memory_order_release,
                      std::memory_order_relaxed));
    }
    std::shared_ptr<T> pop()
    {
        counted_node_ptr old_head=
            head.load(std::memory_order_relaxed);
        for(;;)
        {
            increase_head_count(old_head);
            node* const ptr=old_head.ptr;
            if(!ptr)
            {
                return std::shared_ptr<T>();
            }
            if(head.compare_exchange_strong(
                   old_head,ptr->next,std::memory_order_relaxed))
            {
                std::shared_ptr<T> res;
                res.swap(ptr->data);
                int const count_increase=old_head.external_count-2;
                if(ptr->internal_count.fetch_add(
                       count_increase,std::memory_order_release)==-count_increase)
                {
                    delete ptr;
                }
                return res;
            }
            else if(ptr->internal_count.fetch_add(
                        -1,std::memory_order_relaxed)==1)
            {
                ptr->internal_count.load(std::memory_order_acquire);
                delete ptr;
            }
        }
    }
};

我担心功能

void increase_head_count(counted_node_ptr& old_counter)
{
    counted_node_ptr new_counter;
    do
    {
        new_counter=old_counter;
        ++new_counter.external_count;
    }
    while(!head.compare_exchange_strong(
              old_counter,new_counter,
              std::memory_order_acquire,
              std::memory_order_relaxed));
    old_counter.external_count=new_counter.external_count;
}

看来,new_counter = old_counter;当old_counter.ptr被另一个线程删除时,可以执行。

那么,有人可以确认或拒绝,这个堆栈实现在c ++ 11中是严格错误的吗?

2 个答案:

答案 0 :(得分:2)

我认为该实现还有其他问题: 让我们假设两个线程正在一个非空无锁堆栈上工作:

  1. 线程A在代码行之后调用push()添加一个新节点 new_node.ptr-> next = head.load(std :: memory_order_relaxed);已经 执行后,线程A进入睡眠状态; <​​/ li>
  2. 线程B在代码行之后调用pop()删除旧节点 增加头数(旧头);已执行,线程B进入 睡觉;
  3. 线程A继续运行,并且发现头节点的外部引用计数不为1,但是该信息将被忽略,并且新节点将作为新头添加到堆栈中;
  4. 线程B继续运行,并且head.compare_exchange_strong()将失败,并且将执行ptr-> internal_count.fetch_add(-1,std :: memory_order_relaxed),这将导致旧头的外部引用计数指针仍为2,但内部引用计数为-1。
  5. 所以无锁堆栈坏了!

谁可以帮助检查这是否是一个真正的问题?

答案 1 :(得分:0)

这是可能的,但不是问题,因为routes.MapRoute( name: "PreTest", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( name: "Test", url: "{controller}/{*.}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); 调用会检测到它并丢弃compare_exchange