72. mutable Keyword

Question.2

A thread-safe DataStore has a const getter that needs to lock a mutex:

class DataStore {
   int data;
   mutable std::mutex mtx;
public:
   int get_data() const {
       std::lock_guard<std::mutex> lock(mtx);
       return data;
   }
};

Why must mtx be mutable?

Need Help? Refer to the Quick Guide below

Select Answer