1

According to the C++ standard, std::atomic can be combined with any trivially copyable type. However, GCC produces the following error messages:

#include <atomic>
struct TriviallyCopyableType {
  int a, b, c, d;
};
int main() {
  std::atomic<TriviallyCopyableType> a;
  a.store({});      // undefined reference to `__atomic_store_16'
  a.is_lock_free(); // undefined reference to `__atomic_is_lock_free'
}

Clang and Microsoft's compiler do not complain. Am I doing something wrong? Is this a known problem? After all, atomic operations were implemented years ago in GCC 4.4. Are there any workarounds other than using a different compiler? Since Clang implements std::atomic<TriviallyCopyableType> even lock-free, I do not want to use explicit locking.

user1494080
  • 2,064
  • 2
  • 17
  • 36
  • 3
    fine on gcc 6.3. I think you need a newer compiler. Yours is ancient. – Richard Hodges Dec 25 '16 at 21:20
  • Well, I wouldn't call 5.4 ancient... – user1494080 Dec 25 '16 at 21:23
  • 3
    It seems [this answer](http://stackoverflow.com/a/15204653/1120273) is still relevant. Possibly, you need to includue `-latomic`. – Dietmar Kühl Dec 25 '16 at 21:34
  • 1
    @user1494080 forgive me. You wrote 4.4 – Richard Hodges Dec 25 '16 at 21:42
  • @DietmarKühl That's the solution. GCC is bundled with the correct library, I only had to specify `-latomic`. I wouldn't have thought that I have to explicitly specify libraries in order to compile code using only standard constructs. Would you like to post your comment as an answer? – user1494080 Dec 26 '16 at 02:38
  • 1
    If you're using that code, you should probably use `-mcx16` on x86-64 to let the compiler use `cmpxchg16b`. That could enable lock-free 16-byte ops. See also http://stackoverflow.com/questions/38984153/implement-aba-counter-with-c11-cas/38991835#38991835. – Peter Cordes Dec 28 '16 at 00:12

1 Answers1

1

This answer is compiled from the comments.

You need to explicitly link the atomic operations library with your program by specifying -latomic on the command line.

-mcx16 may enable lock-free atomic operations on 128-bit data types.

user1494080
  • 2,064
  • 2
  • 17
  • 36