Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::atomic with custom class (C++ 11)

Tags:

I am using std::atomic with a custom class in my library. All works fine with MSVC, but now that i'm trying to get it to run on macOS, i get a linker error:

undefined symbols for architecture x86_64: "__atomic_store", referenced from: _main in main.o

I've created some test code to replicate this

#include <iostream> #include <atomic>  using namespace std;  class Vec {     public:     int x, y, z;     Vec() { x = y = z = 0; } };  std::atomic<Vec> x;   int main() {   Vec a;   x = a;   cin.get();     return 0; } 

Of course this example doesn't make much sense, but it's the shortest I could come up with. It does run in VS2012, but not in xcode (giving me the linker error shown above).

So what's going on? Am I abusing std::atomic here? The library I'm working on is heavily multithreaded and used for audio processing. So if I'm not using std::atomic in the correct way, it is not really showing, because performance is very good and I don't have any threading problems with it. Or is xcode perhaps lacking something?

Update:

I've checked andrey's answer because it has the most information, although all 3 answers are useful. I'm no expert in this (obviously) but it seems that VS2012 goes beyond what should be implemented in C++11.

So how to go from here? I see a few options.

  1. Don't use atomic for this class. In my particular case this would be very hard because my vector class is used all over the code. Locking and unlocking mutexes would probably slow things down a lot.
  2. Implement the functions myself for atomic. That looks very complicated to me. I'll save it as the last option.
  3. See if something can be done with boost::atomic. This seems to work at first glance. I have to do more tests on it though.
like image 263
yvan vander sanden Avatar asked Apr 08 '13 18:04

yvan vander sanden


People also ask

What is std :: atomic in C++?

Each instantiation and full specialization of the std::atomic template defines an atomic type. Objects of atomic types are the only C++ objects that are free from data races; that is, if one thread writes to an atomic object while another thread reads from it, the behavior is well-defined.

Is atomic thread safe C++?

In order to solve this problem, C++ offers atomic variables that are thread-safe. The atomic type is implemented using mutex locks. If one thread acquires the mutex lock, then no other thread can acquire it until it is released by that particular thread.

Is std :: swap Atomic?

It is not atomic. Atomic operations are not cheap and 99% of the time you do not need the atomicity. There are (IIRC) some other means to get atomic operations but std::swap() is not one of them.

Why is std atomic not movable?

std::atomic is not copyable or movable because its copy constructor is deleted and no move constructor is defined.


1 Answers

As described in http://en.cppreference.com/w/cpp/atomic/atomic:

The standard library provides full specializations of the std::atomic template for the following types:

1) One specialization for the type bool and its typedef
2) Specializations and typedefs for integral types
3) std::atomic for all pointer types

What about the Boost.Atomic. As described in Boost.Atomic limitations:

Using non-POD-classes as template parameter to atomic results in undefined behavior.

like image 132
Andrey Volk Avatar answered Sep 28 '22 22:09

Andrey Volk