1

I just listened to this talk about how const actually means bitwise immutable OR thread safe in and after c++11. Furthermore we should guarantee const functions to be thread safe. This is all well and good until I tried to implement it. Look at this example:

class Foo {
public:
    void increment() const {
        std::lock_guard<std::mutex> lk(m);
        hiddenVal+=1;
    }
private:
    std::mutex m;
    int hiddenVal=0;
};

This code is actually not going to compile because we are modifying the member variables even though the function is thread safe. So in the end to make the function actually valid we must declare the variables like this:

mutable std::mutex m;
mutable int hiddenVal=0;

Now I agree that mutex is inherently thread safe so it makes sense for it to be mutable but hiddenVal is in no way thread safe so why are we forced to also make it mutable? Now imagine this on a larger scale. Every function called from a const function must also be const. Every variable modified in const functions must be mutable. This honestly seems like an anti-pattern.

A.Hristov
  • 481
  • 1
  • 4
  • 13
  • _"but `hiddenVal` is in no way thread safe"_ Isn't (incrementing) `hiddenVal `thread safe because it is protected by the mutex? I am afraid I don't get your question / concerns. Also I don't see a point to make the `increment()` function `const`, besides that that value should also be kept trackable and _mutable_ even for `const` instances of `Foo`. – πάντα ῥεῖ Oct 02 '20 at 06:32
  • I meant that it isn't inherently thread safe like the mutex so putting it as mutable seems like a bad practice but is required for compilation. It's true that this example doesn't make much sense but if the standard is to be followed thread safe functions must be denoted by the const keyword – A.Hristov Oct 02 '20 at 06:35
  • 3
    I haven't watched the talk but i imagine it's saying that `const` methods should be thread safe rather than all thread safe methods should be `const`. A const method which modifies a member is pretty unexpected – Alan Birtles Oct 02 '20 at 06:37
  • You end up with the fact that C++ is not a fail-save language and offers enough to shoot into you own foot. I tried an alternative (without `mutable` but a simple cast) that made compiler silent although it still is wrong: [Demo on Compiler Explorer](https://godbolt.org/z/9M5s5j). – Scheff's Cat Oct 02 '20 at 06:39
  • @AlanBirtles what you're saying makes a lot of sense. I'll watch it again to see – A.Hristov Oct 02 '20 at 06:43
  • This is the actual one to one duplicate: https://softwareengineering.stackexchange.com/questions/379516/is-the-meaning-of-const-still-thread-safe-in-c11 – A.Hristov Oct 02 '20 at 06:57
  • @Alan Birtles even if method doesn't modify member.. it cannot be thread-safe without mutex even if it only reads from class storage - a partial read may happen if other thread changed accessed value. It's not like call to const method creates a constant copy of object to run method against it (with all owned resources??)? it only means that content of method *assumes* that object didn't change, not that it's actually true. – Swift - Friday Pie Oct 02 '20 at 07:08

0 Answers0