Herb Sutter GotW6: Const and Mutable

This item covers ground Herb already presented in this video. He recommends that const member functions must be one of:

  • truly physically/bitwise const with respect to this object
  • internally synchronized so that if it does perform any actual writes to the object’s data, that data is correctly protected with a mutex or equivalent (or if appropriate are atomic) so that any possible concurrent non-const accesses by multiple callers can’t tell the difference.

Similarly, he asserts that the guidelines taught for C++98, that const means logically const but you had a free rein with internal data, is no longer true for C++11 with its memory model and thread safety specification.

This implies stricter best practice on use of mutable member variables. Any time you need to mutate data in a const member function, you should protect it with a synchronisation object to ensure thread safety.

Leave a comment

Filed under C++, Programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.