15

I have thread-safe Document class representing custom document. It have getters (const functions) and setters to modify it's state. All these functions are mutex-protected to guarantee document will not changed until the method will be completely executed.

But due to QMutex usage, i can't mark state-accessed function as const without mutable usage. Capturing QMutex change it's state.

Is this code correct, or it can be written in a more nice way? Without hacky mutable usage.

class Document
{
    // This method should be const: it changes only mutex
    // and don't touch document state
    bool IsCorrect() const;
    ...
    mutable QMutex m_lock;
};

bool Document::IsCorrect() const
{
    // Capturing mutex object change it!
    QMutexLocker lock( &m_lock );
    ... Const-aware code
    bool result = ( m_context != NULL );
    ...
    return result;
}
6
  • 9
    This use of mutable is not "hacky"... it's exactly what mutable is there for (but not exclusively - e.g. caching results, instrumentation are equally valid uses). Aug 27, 2014 at 7:46
  • Another example of mutable could be a cache (internal representation of external state) that can be refreshed when read out, so even if a reading function is const, the cache can change, but that doesn't mean that the state of the object has changed.
    – stefaanv
    Aug 27, 2014 at 7:57
  • @TonyD My chief says something like "const function must not changing anything - and mutex is a part of Document state, changing it implicitly is confusing". Should i explain him about logical and bitwise constness?
    – eraxillan
    Aug 27, 2014 at 8:35
  • @eraxillan Of course a mutex could also be used for other reasons than providing thread-safety. In such cases a mutex could indeed contribute to the logical "state" of the class. But if used safely, its totally okay to use mutable for the mutex and mark the method as const. Your boss is just wrong making such a strict claim.
    – oxygene
    Aug 27, 2014 at 8:41
  • 2
    @eraxillan: Yes - logical/observable state and all that - if you've the patience. Finding a gentle online tutorial or talk to share might make it easier, and add authority if he's difficult. Otherwise, if this "chief" is in a programming role suggest a career change; otherwise suggest they butt out and leave it to the professionals ;-). Aug 27, 2014 at 8:42

1 Answer 1

25

Herb Sutter has an excellent talk on Channel9 about this topic.

The point relevant here is that in C++11, mutable has gained a new meaning regarding thread safety: "this data member is internally synchronised." That's precisely what a mutex is. Herb even says in that talk when talking about a mutex: "it wants to be mutable."

So having a mutable mutex and accessing it from a const member function is not hacky at all, and it's in fact one of the primary intended uses for mutable.

1
  • Ok, i am calm now. However, i find mutex-based design is not the best one. But this another question.
    – eraxillan
    Aug 27, 2014 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.