【问题标题】:invalid initialization of non-const reference of type ‘Node*&’ from an rvalue of type ‘std::atomic<Node*>::__pointer_type {aka Node*}’从“std::atomic<Node*>::__pointer_type {aka Node*}”类型的右值对“Node*&”类型的非常量引用进行无效初始化
【发布时间】:2017-10-13 14:28:30
【问题描述】:

我有以下sn-p的代码:

struct Node {
   int data;
   Node *next;
};

atomic<Node*> head;
atomic<Node*> temp1 = head.load();
..
Node *temp2 = new Node;
//initialise values
head.compare_exchange_strong(temp1, temp2);

但是,我收到以下错误:

从“std::atomic::__pointer_type {aka Node*}”类型的右值初始化“Node*&”类型的非常量引用无效。

我没有在这里得到哪个引用是恒定的。任何帮助将不胜感激。

【问题讨论】:

    标签: c++11 nonblocking lock-free


    【解决方案1】:

    简单的答案是 temp1 应该是 Node*,而不是 Atomic, 因为 cmp/xchg 需要两个简单的类型变量。

    但我真的不明白你想要达到什么目的。 当然,如果您希望 next 受到线程保护,那么它应该在结构内声明为 Atomic 吗?

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-03
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多