From Jason Turner
Watch on YouTube

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.

Resources

Tags