14

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;
}
eraxillan
  • 1,552
  • 1
  • 19
  • 40
  • 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). – Tony Delroy Aug 27 '14 at 07: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 '14 at 07: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 '14 at 08: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 '14 at 08: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 ;-). – Tony Delroy Aug 27 '14 at 08:42
  • @TonyD :D However, i will try to explain it in a spare way, using Herb Sutter as a weapon... This is a really famous company, i don't wont to lose this job ;( – eraxillan Aug 27 '14 at 08:53

1 Answers1

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.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455