The C ++ Standard library provides the following guarantees about its types, unless otherwise specified:
(1) Read operations (i.e., work with an object const) are thread safe. This means that several threads can read from the object at the same time without race conditions, if no thread writes (using the non operation const) to the object at the same time.
(2) Multiple threads can read and write arbitrary objects at a time, as long as each object is available only one stream at a time.
The standard library requires the same guarantees for the types of users. (You can read about it in GotW # 95 or see Herb at C ++ and beyond 2012 explaining this.)
Now my question is this: if operator()of std::functionis a member function const, it should be thread safe. If the functor that passed during the construction has a const member function operator(), then the object std::functioncan consider it thread safe and simply redirect the call. However, if the functor passed to it during construction is mutable operator(), then this operation should not be thread safe, but std::functionit should still be, because the call operator remains const. Therefore, it std::functionmust externally synchronize calls with the stored mutable functor and, therefore, use the mutex. This means that the performance overhead is in the case of passing mutable lambdas to the constructor std::function.
? , ?