Is this function atomic flux-safe

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?

+4
source share
2 answers

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:

 uint32 increase_and_get() { return count_++; } 
+9
source

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.

This is exactly what std::atomic needs to do.

+4
source

All Articles