Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use std::atomic<std::array<>>? [duplicate]

In 29.5 Atomic types of the C++ Standard November 2014 working draft it states:

  1. There is a generic class template atomic. The type of the template argument T shall be trivially copyable (3.9). [ Note: Type arguments that are not also statically initializable may be difficult to use. —end note ]

So - as far as I can tell - this:

#include <atomic>

struct Message {
    unsigned long int a;
    unsigned long int b;
};

std::atomic<Message> sharedState;

int main() {    
    Message tmp{1,2};       
    sharedState.store(tmp);         
    Message tmp2=sharedState.load();
}

should be perfectly valid standard c++14 (and also c++11) code. However, if I don't link libatomic manually, the command

g++ -std=c++14 <filename>

gives - at least on Fedora 22 (gcc 5.1) - the following linking error:

/tmp/ccdiWWQi.o: In function `std::atomic<Message>::store(Message, std::memory_order)':
main.cpp:(.text._ZNSt6atomicI7MessageE5storeES0_St12memory_order[_ZNSt6atomicI7MessageE5storeES0_St12memory_order]+0x3f): undefined reference to `__atomic_store_16'
/tmp/ccdiWWQi.o: In function `std::atomic<Message>::load(std::memory_order) const':
main.cpp:(.text._ZNKSt6atomicI7MessageE4loadESt12memory_order[_ZNKSt6atomicI7MessageE4loadESt12memory_order]+0x1c): undefined reference to `__atomic_load_16'
collect2: error: ld returned 1 exit status

If I write

g++ -std=c++14 -latomic <filename>

everything is fine. I know that the standard doesn't say anything about compiler flags or libraries that have to be included, but so far I thought that any standard conformant, single file code can be compiled via the first command.

So why doesn't that apply to my example code? Is there a rational why -latomic is still necessary, or is it just something that hasn't been addressed by the compiler maintainers, yet?

like image 999
MikeMB Avatar asked Nov 21 '22 01:11

MikeMB


2 Answers

Relevant reading on the GCC homepage on how and why GCC makes library calls in certain cases regarding <atomic> in the first place.

GCC and libstdc++ are only losely coupled. libatomic is the domain of the library, not the compiler -- and you can use GCC with a different library (which might provide the necessary definitions for <atomic> in its main proper, or under a different name), so GCC cannot just assume -latomic.

Also:

GCC 4.7 does not include a library implementation as the API has not been firmly established.

The same page claims that GCC 4.8 shall provide such a library implementation, but plans are the first victims of war. I'd guess the reason for -latomic still being necessary can be found in that vicinity.

Besides...

...so far I thought that any standard conformant, single file code can be compiled via the first command.

...-lm has been around for quite some time if you're using math functions.

like image 137
DevSolar Avatar answered Nov 22 '22 14:11

DevSolar


I know that the standard doesn't say anything about compiler flags or libraries that have to be included

Right.

but so far I thought that any standard conformant, single file code can be compiled via the first command.

Well, no. As you just said, there is no particular reason to assume this. Consider also that GCC extensions are enabled by default.

That being said, it seems self-evident that the intention is to make -latomic a default part of the runtime when it's settled down a bit.

like image 32
Lightness Races in Orbit Avatar answered Nov 22 '22 15:11

Lightness Races in Orbit