【问题标题】:Atomic struct containing pointer包含指针的原子结构
【发布时间】:2020-11-25 19:14:12
【问题描述】:
#include <atomic>
#include <iostream>

using namespace std;

struct Simple{
    int a = 0;
    int b = 0;
};

struct WithPointer{
    int *a = nullptr;
    int b = 0;
};

int main(int argc, char const *argv[])
{
    atomic<Simple> simple;
    cout<<simple.is_lock_free()<<"\n";
    
    atomic<Simple*> simple_p;
    cout<<simple_p.is_lock_free()<<"\n";

    atomic<WithPointer> with_pointer;
    cout<<with_pointer.is_lock_free()<<"\n";

    return 0;
}

此示例适用于 Simple 结构,但不适用于 WithPointer 结构。 我得到以下编译错误,为什么? 我该怎么办。

g++ main.cpp
/usr/bin/ld: /tmp/cc49YEoR.o: in function `std::atomic<WithPointer>::is_lock_free() const':
1a.cpp:(.text._ZNKSt6atomicI11WithPointerE12is_lock_freeEv[_ZNKSt6atomicI11WithPointerE12is_lock_freeEv]+0x1d): undefined reference to `__atomic_is_lock_free'
collect2: error: ld returned 1 exit status

【问题讨论】:

  • 相关/欺骗stackoverflow.com/questions/44891523/…虽然没有得到答复。
  • 我在加载其他原子操作时也遇到同样的错误
  • 看我的回答。与-latomic 链接应该修复所有未定义的引用。

标签: c++ g++ locking atomic


【解决方案1】:

您需要在 clang 和 gcc 上使用 -latomic 标志编译程序。 demo.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 2021-07-28
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多