Atomic struct containing pointer

拟墨画扇 提交于 2020-08-19 12:12:09

问题


#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;
}

This example works fine for the Simple struct but not for the WithPointer struct. I get the following compile error, why? What can I do.

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

回答1:


You need to compile the program with the -latomic flag on clang and gcc. demo.



来源:https://stackoverflow.com/questions/44891523/undefined-reference-atomic-is-lock-free-with-xcode-8-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!