Note that there are some explanatory texts on larger screens.

plurals
  1. POAre C++11 objects potentially slower in multi-threaded environments because of the new const?
    primarykey
    data
    text
    <p>According to Herb Sutter (<a href="http://isocpp.org/blog/2012/12/you-dont-know-const-and-mutable-herb-sutter" rel="nofollow">http://isocpp.org/blog/2012/12/you-dont-know-const-and-mutable-herb-sutter</a>), in C++11 const methods must not alter the object bit-wise, or must perform internal synchronization (e.g. using a mutex) if they have mutable data members.</p> <p>Suppose I have a global object that I'm accessing from multiple threads and suppose it has mutable members. For the sake of argument, let's assume that we cannot modify the source of the class (it's provided by a third-party).</p> <p>In C++98 these threads would use a global mutex to synchronize access to this object. So, an access would require a single mutex lock/unlock.</p> <p>However, in C++11, any const member function call on this object will invoke internal synchronization as well, so potentially, a single const function call on this object will cost 2 lock/unlock operations (or more, depending on how many functions you call from a single thread). Note that the global mutex is still needed, because const doesn't seem to do anything for writers (except possibly slowing them down as well, if one of the non-const methods calls a const method).</p> <p>So, my question is: If all of our classes have to be this way in C++ (at least to be usable by STL), doesn't this lead to excessive synchronization measures?</p> <p>Thanks</p> <p><strong>Edit:</strong> Some clarifications:</p> <ol> <li><p>It seems that in C++11, you cannot use a class with the standard library unless its const member functions are internally synchronized (or do not perform any writes).</p></li> <li><p>While C++11 doesn't automatically add any synchronization code itself, a standard-library-compliant class doesn't need synchronization in C++98, but needs it in C++11. So, in C++98 you can get away with not doing any internal synchronization for mutable members, but in C++11 you can't.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload