Monday, 15 July 2013

c# - Threadsafe Lazy Class -


I have class lazy which freely evaluates expressions:

< Pre> public sealed class lazy & lt; T & gt; {Funk & lt; T & gt; GetValue; T value; Public lazy (funk & lt; T & gt; f) {getValue = () => {Lock (getValue) {value = f (); GetValue = () = & gt; value; } return value; }; } Public T-Force () {return getValue (); }}

Actually, after trying to evaluate the object locking overhead, I'm trying to get the getValue with a second function I change with allocation.

This apparently works in my trial, but I have no way of knowing that it will blow into production.

Is my square safe thread? If not, what can be done to guarantee yarn protection?

To fully function using a flag or guard value for the actual value Skip to re-evaluate? I.e .:

  Public sealed class lazy & lt; T & gt; {Funk & lt; T & gt; F; T value; Unstable bool computed = false; Zero GetValue () {LockObject} {value = f (); Calculation = true; }} Public lazy (funk & lt; t & gt; f) {this.f = f; } Public T Force (if (calculated!) GetValue (); Quantity of Return;}}  

No comments:

Post a Comment