Possible way to achieve lazy loading with const objects

Jonathan M Davis jmdavisProg at gmx.com
Thu Sep 29 13:11:26 PDT 2011


On Thursday, September 29, 2011 16:01:05 Steven Schveighoffer wrote:
> 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?

As I said, there was no such stipulation it the original proposal or 
discussion, and without that stipulation, the assumption that two runs of the 
same initializer function will result in the same value does not necessarily 
hold, and a mutex will be required, but if we require that stipulation and 
therefore insist that the initializer be pure, then yes, we can avoid the 
mutex.

Now, that's that much more limiting and and makes the whole proposal that much 
less useful and therefore that much less worth the added complexity, but it 
could be that such a stipulation would be required for it to work. Regardless, 
I think that it's pretty clear that a mechanism for lazy loading such as I 
have proposed does not provide enough benefit to be worth the added complexity. 
And since it doesn't seem to appease the people who wanted something like it 
at all (since what they really want is full-on logical const), then I don't 
see much point to it anyway.

- Jonathan M Davis


More information about the Digitalmars-d mailing list