A C++ Cache class has a const get() that updates a hit counter and an internal map; how do you make it correct and thread-safe?
answer
- logical versus bitwise const
- mutable lifts const per member
- const calls look like concurrent reads
- mutable mutex plus atomic counter
basics
~20 sDeclare the map and counter mutable so a const get() may update them, then synchronise them: a mutable std::mutex around the map, a std::atomic counter. Callers treat const calls as safe concurrent reads, so unsynchronised mutable state races.
solid answer
~40 s`get()` is **logically** const: the cache's observable answers never change, only its internals do, so `mutable` is the right tool. `mutable` lets a const member function modify that one member; it does nothing for threads. Callers sharing a `const Cache&` across threads assume, as the standard library guarantees for its own types, that const calls do not modify shared state, so two threads in `get()` writing an unprotected `mutable long hits_` and `mutable std::map` is a data race and undefined behaviour; ThreadSanitizer reports it. The fix is to synchronise every mutable member: a `mutable std::mutex` locked with `std::lock_guard` (the mutex must be `mutable` because `lock()` is a non-const function), and a `mutable std::atomic<long>` for the counter. The cost: `std::mutex` is neither copyable nor movable, so `Cache` loses its implicit copy and move operations.
code
cpp · 31 lines#include <atomic>
#include <cstdio>
#include <map>
#include <mutex>
#include <string>
#include <thread>
class Cache {
public:
std::string get(int key) const { // logically const
hits_.fetch_add(1, std::memory_order_relaxed);
std::lock_guard<std::mutex> lock(mutex_);
auto it = map_.find(key);
if (it == map_.end())
it = map_.emplace(key, std::to_string(key * key)).first;
return it->second; // copy out while locked
}
long hits() const { return hits_.load(); }
private:
mutable std::mutex mutex_;
mutable std::map<int, std::string> map_;
mutable std::atomic<long> hits_{0};
};
int main() {
const Cache cache;
auto work = [&cache] { for (int i = 0; i < 1000; ++i) cache.get(i % 10); };
std::thread a(work), b(work), c(work), d(work);
a.join(); b.join(); c.join(); d.join();
std::printf("hits=%ld value=%s\n", cache.hits(), cache.get(7).c_str());
}go deeper
Recall that mutable lets a const member function change one specific member, and that it was invented for caches and counters.
Explain logical versus bitwise const and why the mutex itself must be mutable, since lock() is a non-const member function.
Show that const signals safe concurrent reads, prove the race with ThreadSanitizer, and fix it with a mutable mutex and an atomic while accounting for lost copyability.
Weigh a single mutex against a shared_mutex or per-thread caches by hit rate and contention, and decide which types in a codebase must honour const-means-thread-safe.
## Bitwise const and logical const A const member function in C++ is checked **bitwise**: the compiler rejects any write to a non-static data member reached through `this`. Many useful types are const only **logically**: their observable behaviour does not change, but their internals do. A memoising `Cache::get(int key) const` returns the same value for the same key every time, yet on a miss it inserts into its map and on every call it bumps a hit counter. The keyword that reconciles the two views is **`mutable`**. ## What mutable does, and what it cannot do The standard describes the `mutable` specifier on a data member as nullifying a `const` applied to the containing object, permitting modification of that member even though the rest of the object is const. Its limits come from the same wording: - It applies only to a **non-static data member** whose type is neither const-qualified nor a reference; a static data member cannot be `mutable`. - It works even for an object **defined `const`**, so `const Cache cache;` may update its mutable members without undefined behaviour, unlike `const_cast` on `this`. - It says **nothing about threads**. A `mutable` member is still ordinary memory, and concurrent unsynchronised writes to it race. ## Why const implies "safe to read concurrently" The C++ standard library's data-race rules require that a library function not modify objects reachable by other threads unless they are reached through the function's **non-const** arguments, including `this`. That is why several threads may call `size()` or `find()` on one shared `std::map` without locking, as long as nobody calls a non-const member. Code that receives a `const Cache&` brings the same expectation to user types. A `get()` that is `const` but writes unprotected `mutable` state breaks it silently: the signature says "read", the body writes. Running the naive version (a `mutable long hits_` incremented with `++hits_` and a `mutable std::map` updated with `emplace`) from two threads under `-fsanitize=thread` gives: ```plaintext WARNING: ThreadSanitizer: data race (pid=86794) Write of size 8 at 0x00016f9ce928 by thread T2: #0 Cache::get(int) const racy.cpp:9 SUMMARY: ThreadSanitizer: data race racy.cpp:9 in Cache::get(int) const ``` The hit count that program prints means nothing: a data race is undefined behaviour, and concurrent `emplace` calls can corrupt the map's internal structure as well. ## Making get() correct 1. Mark the map `mutable` and guard it with a **`mutable std::mutex`**. The mutex needs `mutable` because `std::mutex::lock()` is a non-const member function. 2. Take the lock with `std::lock_guard<std::mutex>` (or `std::scoped_lock`) for the whole find-or-insert sequence, because the lookup and the insert must be one critical section. 3. Make the counter a **`mutable std::atomic<long>`** so it can be bumped without the lock; `memory_order_relaxed` suffices for a statistic that orders nothing else. 4. Return the value **by copy**, not as a reference into the map, so the caller does not hold a reference that another thread's insert could race with. The code example is this design; it runs clean under ThreadSanitizer and prints `hits=4000 value=49`. ## Costs and alternatives | Choice | Benefit | Cost | |---|---|---| | `mutable std::mutex` | simple, correct | every `get()` serialises; `Cache` becomes non-copyable and non-movable | | `mutable std::shared_mutex` | concurrent hits under a shared lock | a miss must release, take the exclusive lock and look up again | | per-thread caches | no locking | memory per thread and no shared warm-up | | non-const `get()` | honest signature | const callers cannot use it, and concurrent calls still need a lock | The copy cost deserves a sentence in an interview: `std::mutex` deletes its copy operations and declares no move operations, so `Cache` can no longer be copied or moved by default. If `Cache` must be copyable, write the copy constructor to lock the source and copy the map, leaving the new mutex freshly constructed; how special members are generated is covered with the rule of zero, three and five. ## How to say it State the two layers separately: `mutable` makes the code **compile and stay defined** for const objects, and synchronisation makes it **thread-safe**. A candidate who offers only `mutable` has fixed the compiler error and kept the race. A strong answer also names the evidence, a ThreadSanitizer run over a concurrent test, and the trade-off the lock introduces, so the interviewer hears both correctness and cost.
- Why can a C++ Cache holding a mutable std::mutex no longer be copied or moved by default?`std::mutex` deletes its copy constructor and copy assignment and declares no move operations. A class's implicitly declared copy and move operations are defined as deleted when a member cannot be copied or moved, so `Cache` loses them. If copies are needed, write a copy constructor that locks the source's mutex, copies the map and counter, and default-constructs its own mutex.
- Would a mutable std::shared_mutex let several threads run a C++ Cache::get() at once?For hits, yes: lookups take a `std::shared_lock` and proceed in parallel. A miss cannot upgrade in place; it must release the shared lock, take a `std::unique_lock`, and search again because another thread may have inserted the key meanwhile. Whether that beats a plain `std::mutex` depends on the hit rate and on how long the critical section is; measure before switching.
- Declaring a member mutable makes access to it thread-safe.
- A const member function is automatically safe to call from several threads.
- const_cast on this is an equivalent and cleaner alternative to mutable.
- The mutex can be an ordinary member because locking does not change it.
- Dropping const from get() removes the data race.