`latomic` flag sometimes not required?
Asked Answered
A

1

-1

I know that the -latomic flag is required for the atomic library to be linked in by GCC. However, for some reason std::atomic<int> doesn't require it to build

build without latomic

while structs do

build requires latomic

what is this difference caused by?

Alvera answered 7/8, 2023 at 18:54 Comment(5)
Please post the code and the errors in the question in form of a minimal reproducible example.Cobbett
@273K all of my examples are linked through Godbolt and are minimally reproducible.Alvera
A plausible explanation is that on your target architecture, atomic operations on int-sized operands are directly mapped onto processor instructions (via intrinsics) and don't need any library code.Gammadion
External links are not searchable on SO, the second code can be reduced to a few lines #include <atomic> struct PropBoardSensorData { float tcTemp1, tcTemp2, tcTemp3; }; int main(){ std::atomic<PropBoardSensorData> atomicData{}; auto newData = atomicData.load(); }Cobbett
@273K "External links are not searchable on SO" Ironically, the external link does contain the code, so the posted message w/ just links is no shorter than one with full code.Foolproof
P
5

If the compiler inlines the __atomic builtins that std::atomic uses, the object file won't contain any references to functions that libatomic defines.

This is normal for most architectures with atomic<size_t> or smaller, at least with optimization enabled, and sometimes for structs the size of two pointers on some architectures. (But not x86-64 since GCC7, where it chooses not to inline lock cmpxchg16b even if you compile with -mcx16, and chooses not to report it as is_lock_free. https://gcc.gnu.org/ml/gcc-patches/2017-01/msg02344.html)

In your case, as you can see by mousing over the source and looking at the asm, auto newData = atomicData.load(); compiled to mov DWORD PTR [rbp-4], eax even in your debug built. (With a huge amount of clutter since you disabled optimization.)

Papageno answered 7/8, 2023 at 19:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.