-1

there are a bunch of fast memory barrier atomic ops in GCC linux like __sync_fetch_and_and etc.

I want to fetch and set a pointer value in memory so that it is guaranteed to be the value set when gotten as sometimes the assignment instructions are not atomic. I don't want a heavyweight mutex for this as it is too fat.

what is the best way ?

peterk
  • 5,136
  • 6
  • 33
  • 47
  • I think the platform matters, too. I seem to recall some things were implemented differently or missing on Aarch64. Also see [GCC inline assembly and the removal of -mcx16](https://gcc.gnu.org/ml/gcc-help/2017-05/msg00187.html) on the GCC mailing list. – jww Jan 03 '18 at 09:33

1 Answers1

0

Do not use atomic buildings in GCC. Use std::atomic, std::atomic_compare_exchange_* for your purposes. Google them on https://cppreference.com.

Information about what is the best you can learn in answers to the similar question Which is more efficient, basic mutex lock or atomic integer?

273K
  • 29,503
  • 10
  • 41
  • 64
  • sort of an answer :) why are the GCC intrinsics bad other than being compiler specific inline code? I would think their implementation of std:atomic are templated wrappers around them. – peterk Jan 04 '18 at 02:51