std::atomic is new feature introduced by c++11 but I can\'t find much tutorial on how to use it correctly. So are the following practice common and efficient?
One p
Your code is certainly wrong and bound to do something funny. If things go really bad it might do what you think it is intended to do. I wouldn't go as far as understanding how to properly use e.g. CAS but you would use std::atomic something like this:
std::atomic value(0);
uint8_t oldvalue, newvalue;
do
{
oldvalue = value.load();
newvalue = f(oldvalue);
}
while (!value.compare_exchange_strong(oldvalue, newvalue));
So far my personal policy is to stay away from any of this lock-free stuff and leave it to people who know what they are doing. I would use atomic_flag and possibly counters and that is about as far as I'd go. Conceptually I understand how this lock-free stuff work but I also understand that there are way too many things which can go wrong if you are not extremely careful.