Summary
In this episode, Jason explores the mutable keyword in C++ and explains why it should generally be avoided. He demonstrates how mutable allows member variables to be modified inside const member functions, which breaks the thread-safety guarantees expected from const methods. Using ThreadSanitizer, he shows how a seemingly innocent caching implementation with mutable can lead to data races when accessed from multiple threads. Jason references the C++ standard clause requiring that objects shouldn't be modified in const contexts when accessed from multiple threads, and suggests alternatives like pre-computing values instead of using mutable caches.
Related C++ Standard Sections
This episode covers topics found in these sections of the C++ standard:
-
[class.static.data]57% match -
[res.on.data.races]55% match -
[dcl.stc]49% match