I was put onto this video:

http://channel9.msdn.com/posts/C-and-Beyond-2012-Herb-Sutter-You-dont-know-blank-and-blank

By https://codereview.stackexchange.com/users/39810/glampert

Because I asked this question:

https://codereview.stackexchange.com/questions/82616/const-cast-of-stdmutex-in-get-member

Marking a class std::mutex as mutable is the right answer to my original question and the video is an excellent and enjoyable discussion around that. I am grateful to glampert.

However I disagree with the premise of the talk that "const == thread-safe" (see time-point 16:00).

Below is my proposal for how we should interpret const in a multi-threaded world. It's not as simple as Herb's proposal but (let's face it) if you think any of the answers in multi-threading are simple you've completely failed to grasp the challenge of multi-threading.

Comments please. What do you think const means for multi-threaded programs? Has its meaning fundamentally changed?

const means logically unmodified by dereferences through the const reference in question.

Multi-threading footnote #1: In objects that form part of the shared state of a multi-threaded program “logically unmodified” will mean “logically const member functions only perform reads on the object” and so any implementation that actually performs writes (such as cached values, instrumentation and even employs synchronization mutexes) needs to adequately make them thread-safe to maintain the ‘logical’ appearance of ‘only reads’.

Single-threading footnote : Of course even in a single threaded application const is by no means a guarantee that an object won’t change during the lifetime of a given const reference. There may be non-const references (aliases) and even a single threaded program might use to modify an object which has one or more const references.

Multi-threaded footnote #2: That lack of guarantee that an object will not be modified just because there is a const reference to it is true in spades for multi-threaded applications. So in many senses – no change there.

I think my greatest concern is that through I am sure Herb Sutter would confirm them as dreadful fallacies, I fear that reading const as ‘thread-safe’ leads too easily to:

  1. The fallacy that adding std::lock_guard guard(this->mutex) to the start of all methods and the destructor of a class somehow make any use of it ‘thread-safe’.
  2. The fallacy that if you only call const methods on a class your application has a thread-safe guarantee and you can complain to the implementer if not.
  3. Finally, the fallacy that if you don’t modify any mutable members and don’t cast away const anywhere you don’t need to worry about thread-safety because (apparently) “const means thread-safe”.

Fallacy 1 is best exemplified by supposing you can get any sense iterating through a collection that is being modified by another thread just because you know the methods are lock-guarded.

Fallacy 2 pushes a potentially onerous (and even impossible) burden on to implementers who can (of course) add lock-guards to all their methods but may (at best) be wasting time and (at worst) just setting you up for fallacy 1.

Fallacy 3 is the fallacy that thread-safety is all about competing for simultaneous access. Memory caching however means that to obtain a coherent state within a const method there may still be a requirement to ensure some kind of memory barrier has been performed (e.g. at least “acquire”).

closed as unclear what you're asking by user40980, durron597, user22815, Ixrec, gnat Aug 28 '15 at 0:11

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    What is the question you are asking us, here? This reads more like a blog post. – user40980 Feb 26 '15 at 12:19
  • @MichaelT: Do you think const means for multi-threaded programs? Has its meaning fundamentally changed? I don't and I think snappy answers make a splash but actually things a more complicated than that. I must say I got the impression Programmers Stack Exchange was the place for more open and general questions. – Persixty Feb 26 '15 at 12:27

Although const as a language feature does not guarantee thread safetyness, lot of the constant functions, by their nature, happens to be threadsafe as they typically just read some constant variables, calculate a result and give it back. This is the bitwise constant mentioned in the video. The cases where internal synchronization is needed (I beleive) are in minority.

You see this by itself creates a (false) security feeling in a programmer who intuitively thinks a function is threadsafe because it is constant.

So I personally support the convention, that if a function is constant, but not bitwise constant, then synchronize inernally to make it threadsafe, because potential users will expect it to be.

This convention is followed in the STL as mentioned in the presentation, which further strengthens the expactation of the users.

The fallacy that if you only call const methods on a class your application has a thread-safe guarantee and you can complain to the implementer if not.

I beleive this is intended. As a user of a library, it does not cause any harm if you are careful about it, but as a library implementor you would better follow this convention.

Fallacy 3 is the fallacy that thread-safety is all about competing for simultaneous access. Memory caching however means that to obtain a coherent state within a const method there may still be a requirement to ensure some kind of memory barrier has been performed (e.g. at least “acquire”).

Maybe const == thread-safe would have been better written as const ==> thread-safe. I don't think Herb Sutter's intention was to state that const is the ultimate silver bullet in thread safetyness. In my interpretation the message is no more, then that when you write a const function, your intention should be to also make it threadsafe.

  • Thanks for your answer. I largely agree. Thinking about your answer I think maybe my problem is calling things thread-safe if they do what the STL does. The blanket offer of STL is simultaneous read safety. For practical purposes that's the weakest possible thread-safety guarantee of any general use. It's a dangerous failure to qualify terms. So if your position was "const ==> simultaneous read thread-safety" I think I agree. The STL isn't safe enough to be called (unqualified) thread-safe or advertised as thread-safe but by implication the video could be interpreted as saying it is. – Persixty Feb 26 '15 at 14:37
  • Well, what would you like to write with a constant function? About the STL I beleive you are missinterpreting the presentation. – Gábor Angyal Feb 26 '15 at 14:53
  • I don't understand your use of the word 'write' in this sentence "Well, what would you like to write with a constant function?" Do you mean what memory do I want to write to? Very little. Obviously there's mutex, reference counters (less needed in shared_ptr world) and lazy cached calculated values (often broken by non-trivial multi-threading). I don't think I've misinterpreted the presentation. I don't think the presenter believes any of the fallacies but I do think a headline of "const = thread-safe" is a misleading mistake put in the presentation to cause a splash. – Persixty Feb 26 '15 at 15:00
  • 1
    Lets not mix the two questions. STL containers are indeed not safe for paralell writing. "const == thread-safe" may indeed be misleading, as I wrote in my answer. I will answer the mutex thing on CR. – Gábor Angyal Feb 26 '15 at 17:04
  • @DanAllen that slide is 16 minutes in, and presented with a lot of context. It's a mnemonic to help remember the presentation, not a headline! If you're going to say "this slide would be misleading out of context and therefore shouldn't be included at all", that precludes the use of any slides, and indeed any non-trivial presentation. – Useless Aug 24 '15 at 13:09

While it does not mean thread-safe per-se, for the standard-library it does:

17.6.5.9 Data race avoidance [res.on.data.races]

1 This section specifies requirements that implementations shall meet to prevent data races (1.10). Every standard library function shall meet each requirement unless otherwise specified. Implementations may prevent data races in cases other than those specified below.
2 A C++ standard library function shall not directly or indirectly access objects (1.10) accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function’s arguments, including this.
3 A C++ standard library function shall not directly or indirectly modify objects (1.10) accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function’s non-const arguments, including this.
[...]

And that's the pattern user-defined types and functions should follow wherever reasonably possible.

  • You had me at 'does not mean thread-safe per-se'. We could summarised const as "Logically read-only". That MUST to mean "Can't logically be the (required) 'at least one of which is a write' in a data race". But I still think it's naive to call that 'thread-safe' because it could imply that all the synchronisation needs to take place on the 'write' and that obviously isn't the case. Imagine a programmer who thinks const members are already thread safe so only puts locks around writes! Nonsense but where the claim leads. – Persixty Feb 27 '15 at 11:38

In C++ for example, a const class or struct can have mutable members. Mutable members are typically used in situations where the logical value of an object doesn't change, but the internal representation my change.

Changing mutable members is obviously not automatically thread safe. Therefore, accessing const objects is not automatically thread safe.

Another example would be a class holding a pointer to a file. I can read or write from that file, even if the instance is const. But these operations are unlikely to be thread safe.

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