I have the following class wrapping a vector of atomic ints ( std::vector< std::atomic_int >
)
The vector is sized correctly at object construction and doesn't change size. There are the usual accessors and mutators for getting, setting the atomic ints, but no guards / mutex.
class MyList
{
std::vector< std::atomic_int > collection_;
static MyList myList_;
public:
MyList() : collection_( MAX_SIZE, 0 ) {}
static MyList& getMyList() { return myList_; }
void set( size_t idx, int val )
{
collection_[idx].store( val, std::memory_order_relaxed );
}
int get( size_t idx ) const
{
return collection_[idx].load( std::memory_order_relaxed );
}
};
I'm minded to suspect that this might not be thread-safe (it is currently running in a single-threaded model without problem), but would appreciate any views. My main concern is with the thread-safety of the unguarded collection, I suppose, rather than the elements of it.