Given shared data protected by a Mutex. What is the appropriate way to read part of the shared data without needing to lock the Mutex? Is using std::atomic_ref an appropriate way as indicated in the example below?
struct A
{
std::mutex mutex;
int counter = 0;
void modify()
{
std::lock_guard<std::mutex> guard(mutex);
// do something with counter
}
int getCounter()
{
return std::atomic_ref<int>(counter).load();
}
};