I was wondering if only primitive std :: atomic data types can be declared in C ++ 11? Is it possible, say, to declare an object of a library class "atomically" mutated or accessible?
For example, I may have
using namespace std::chrono; time_point<high_resolution_clock> foo;
But if these setter and getter methods are called on different threads, I think this can lead to undefined behavior. It would be nice if I could declare foo something like:
std::atomic<time_point<high_resolution_clock>> foo;
... so that all operations on foo will be atomic. There are probably hundreds of such foo variables declared in dozens of classes in the application for my project, and I believe that it would be much more convenient to make the object mutable and accessible "atomic", so to speak, instead of declaring and blocking_guard mutexes everywhere.
Is this not possible, or is there a better approach, or do I really need to use the mutex and lock_guard everywhere?
Update
- Any takers? I fished on the Internet to get decent information, but there are not so many examples with atomic quantities that I canβt be sure to what extent this can be applied.
user1930581
source share