Possible way to achieve lazy loading with const objects

Steven Schveighoffer schveiguy at yahoo.com
Thu Sep 29 13:01:05 PDT 2011


On Thu, 29 Sep 2011 15:23:18 -0400, Jonathan M Davis <jmdavisProg at gmx.com>  
wrote:

> On Thursday, September 29, 2011 15:05:56 Steven Schveighoffer wrote:
>> If all the data the calculated value depends on is immutable, then the  
>> two
>> threads loading the value at the same time will be loading the same
>> value.  If you're writing a 42 to an int from 2 threads, there is no
>> deadlock or race issue.  Writing a 42 over a 42 does not cause any
>> problems.
>
> An excellent point, but that's assuming that the data being used is all
> immutable, and that particular stipulation was not given previously. But  
> if
> that stipulation is there, then you're right. Otherwise, the locking is  
> still
> needed.

Well, the object itself is immutable.  All that is needed is to ensure any  
static data used is also immutable.

Wait, we have that -- pure functions :)

So what if lazy initialization is allowed for immutable as long as the  
function being assigned from is pure?

-Steve


More information about the Digitalmars-d mailing list