I have a C++ library, which supposed to do some computations on multiple threads. I made independent threads code (i.e. there are no shared variables between them), except f
Here is a cleaned up version of your AtomicUInt64 type:
template
struct MobileAtomic
{
std::atomic atomic;
MobileAtomic() : atomic(T()) {}
explicit MobileAtomic ( T const& v ) : atomic ( v ) {}
explicit MobileAtomic ( std::atomic const& a ) : atomic ( a.load() ) {}
MobileAtomic ( MobileAtomic const&other ) : atomic( other.atomic.load() ) {}
MobileAtomic& operator=( MobileAtomic const &other )
{
atomic.store( other.atomic.load() );
return *this;
}
};
typedef MobileAtomic AtomicUInt64;
and use:
AtomicUInt64 value;
myVector->push_back ( value );
or:
AtomicUInt64 value(x);
myVector->push_back ( value );
your problem was you took a std::atomic by value, which causes a copy, which is blocked. Oh, and you failed to return from operator=. I also made some constructors explicit, probably needlessly. And I added const to your copy constructor.
I would also be tempted to add store and load methods to MobileAtomic that forwards to atomic.store and atomic.load.