I am trying to use a basic array of objects using C++11 atomics, i.e. below:
// Atomic array
std::atomic<Object*> *array = new std::atomic<Object*>[10];
// Storing old data
array[0].store(oldObject);
// Store the new data
Object *newObject = new Object();
while(!array[0].compare_exchange_strong(oldObject,newObject));
My question is, can the CAS operation fail only if another thread is modifying the the array at the same index? Or will the CAS operation fail if any thread is trying to modify any location within the array? The latter is the behaviour that I seem to be getting during tests. If so, is there a better way to declare an array so that modifying different individual cells in the array don't effect each other?
Thanks.
IIRC atomic variables on a single cache-line will share the locking (on x86{,_64}).
So maybe extend the array and try variables at each end to test that?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With