Walter is right about transitive readonly - here's the alternative

Janice Caron caron800 at googlemail.com
Fri Sep 14 07:02:13 PDT 2007


On 9/14/07, Steven Schveighoffer <schveiguy at yahoo.com> wrote:
> > And un-consting a mutex is probably the one place where it's
> > absolutely safe to do so, no matter that other threads might be
> > sharing it! Hot dang! That's brilliant!
>
> Hm.. The spec says that the result of casting away const and then modifying
> the variable is "undefined."  I take this to mean that the compiler may do
> some optimizations based on the fact that you have a const object, and then
> your code might not behave as you think in some future version of D :(
>
> Although I think it has been suggested by Walter that casting away const is
> acceptable if "you know what you're doing":

Well, Walter's only mentioned one problem so far, which is that of an
object which the compiler things is const being shared between
multiple threads. Though strictly speaking overriding const is
undefined, we do, in fact, know what would happen. Thread A would make
a modification, and then thread B would see the change. In some cases
(eg. inserting into a linked list), a thread switch partway through
the change would be disasterous. BUT - if you can ensure that no
matter what thread A does, thread B will never see an invalid state,
then you're sorted. So, it's not undefined because "it might not
work", it's undefined because a particular bad thing might happen. The
way I see it, so long as you're sure that that particular bad thing
won't happen, then either (a) it's OK, or (b) there are other
consideration we haven't been told about.

Are there other things that can go wrong we haven't taken into
account? Who knows.



More information about the Digitalmars-d mailing list