gpt4 book ai didi

c++ - 将 16 字节 CAS 与 GCC 结合使用时出现未定义的引用链接器错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:19:48 25 4
gpt4 key购买 nike

我在 Debian 上使用 GCC 4.7.2,每当我尝试使用 <atomic> 时都会遇到链接器错误具有 16 字节值的设施。我正在运行可以支持 CMPXCHG16B 的 x86_64 VM指令 - 但即使我没有必要的硬件,我也不明白为什么这里会产生链接器错误。据我所知,<atomic>如果硬件不支持必要的 CAS 操作,库应该回退到使用常规锁。

无论如何,这里有一个非常简单的测试用例来重现这个问题:

#include <atomic>
#include <cstdint>

struct foo
{
std::uint64_t x;
std::uint64_t y;
};

int main()
{
std::atomic<foo> f1({0,0});
foo f2 = {0,0};
foo f3 = {1,1};
f1.compare_exchange_strong(f2, f3);
}

当我编译它时,我得到:

# g++ test.cpp -o test -std=c++11 -g3
/tmp/ccziKZis.o: In function `std::atomic<foo>::compare_exchange_strong(foo&, foo, std::memory_order, std::memory_order)':
/usr/include/c++/4.7/atomic:259: undefined reference to `__atomic_compare_exchange_16'
collect2: error: ld returned 1 exit status

请注意,如果我更改程序使得 foo只有 8 个字节,我没有收到链接器错误。这是怎么回事?

最佳答案

简单的答案,一旦你知道了:

使用 -mcx16 调用 g++

g++ 文档说:

This option will enable GCC to use CMPXCHG16B instruction in generated code. CMPXCHG16B allows for atomic operations on 128-bit double quadword (or oword) data types. This is useful for high resolution counters that could be updated by multiple processors (or cores). This instruction is generated as part of atomic built-in functions: see *note Atomic Builtins:: for details.

(请注意,这不适用于 clang - 我认为这是一个错误!)

关于c++ - 将 16 字节 CAS 与 GCC 结合使用时出现未定义的引用链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26689162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com