I'm trying to learn how to use atom :)
class foo { static std::atomic<uint32_t> count_; uint32 increase_and_get() { uint32 t = count_++; return t; } }
Is the increase_and_get() function thread safe?
increase_and_get()
Yes, it is safe: the increment is atomic, and the local t cannot be changed by parallel flows. You can simplify your code even further to completely exclude a temporary variable:
t
uint32 increase_and_get() { return count_++; }
Yes, that would be threaded. Assuming, of course, there are no errors in the implementation of std::atomic , but it is usually difficult to obtain the right.
std::atomic
This is exactly what std::atomic needs to do.