title: Does std::function lock a mutex when calling an internal mutable lambda?
body: <p>The C++ Standard library provides the following guarantees about its types unless stated otherwise:</p>
  
  <p>(1) Read operations (i.e. working on a <code>const</code> object) are thread-safe. That means multiple threads may read from an object at the same time without race conditions as long as no thread is writing (applying a non-<code>const</code> operation) to the object at the same time. </p>
  
  <p>(2) Multiple threads may read and write arbitrary objects at the same time as long as each object is only accessed by at most one thread at a time. </p>
  
  <p>The standard library requires the same guarantees by user types. (You can read about this stuff in <a href="http://herbsutter.com/2014/01/13/gotw-95-solution-thread-safety-and-synchronization/" rel="nofollow">GotW #95</a> or watch <a href="http://channel9.msdn.com/posts/C-and-Beyond-2012-Herb-Sutter-You-dont-know-blank-and-blank" rel="nofollow">Herb at C++ and Beyond 2012</a> explaining about this.)</p>
  
  <p>Now my question is, if the following conclusion is correct: Since the <code>operator()</code> of <code>std::function</code> is a <code>const</code> member function, it is required to be thread-safe. If the functor passed in at construction has a const <code>operator()</code> member function, then the <code>std::function</code> object can assume it to be thread-safe and just forward the call. However, if the functor passed to it at construction has a mutable <code>operator()</code>, then this operation does not need to be thread-safe, but <code>std::function</code> still needs to be, because the call operator remains const. Hence <code>std::function</code> must externally synchronize calls to the stored mutable functor and hence use a mutex. This implies a performance overhead in case of passing mutable lambdas to the constructor of an <code>std::function</code>. </p>
  
  <p>Is this reasoning correct? If so, are current implementations compliant? </p>