The Status of Const

Michel Fortin michel.fortin at michelf.com
Fri Aug 13 04:33:47 PDT 2010


On 2010-08-13 01:10:20 -0400, Kagamin <spam at here.lot> said:

> Jonathan M Davis Wrote:
> 
>> The lack of a mutable qualifier seems like it could be
>> another big problem, but I haven't yet written enough code in D to run 
>> into that
>> issue (still I'm sure I'll run into it eventually and be highly annoyed - it's
>> just needed too often in C++ for me not to be happy about it missing in D).
> 
> I've run into it - Exception, generating its error message on demand 
> and caching it in const toString. I think, it's no problem no not have 
> mutable qualifier, because I don't want it often. And you can create a 
> function like void cacheValue!(T)(ref const T storage, T value);

If it does that then it's not thread-safe. If you make an immutable 
exception and attempt to call toString from multiple threads, then you 
create a race condition. Using a write barrier before setting the 
mutable variable we could make the race inconsequential (because in 
this specific case generating and assigning the same string twice won't 
create any problem), but without this you have a race and could get an 
incomplete string from one of the threads.

You might never run into this problem with an Exception, but this is 
certainly not a safe pattern.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list