I want to know how to implement compare_and_swap in c++11. Here's what I tried:
template<typename T>
T compare_and_swap(atomic<T>& reg,T newVal )
{
bool success = false;
T oldVal;
do
{
oldVal = reg.load();
success = reg.compare_exchange_weak(oldVal,newVal);
}while(!success);
return oldVal;
}
Is there a better way to implement this?