Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Why do you need to protect this at all?</p> <p>Changing the referenced instance of a variable is a atomic operation. So what ever you read with <code>get</code> won't be invalid. You can't tell if its the old or the new instance when <code>set</code> is running at the same time. But other then that you should be fine.</p> <p>Partition I, Section 12.6.6 of the CLI spec states: "A conforming CLI shall guarantee that read and write access to properly aligned memory locations no larger than the native word size is atomic when all the write accesses to a location are the same size."</p> <p>And as your variable is a reference type it always has the size of the native word. So your result is never invalid if you do something like this:</p> <pre><code>Private T _item; public T Item { get { return _item; } set { _item = value } } </code></pre> <p>Example if you want to stick to the generic stuff and use it for everything. The approach is the use of a carrier helper class. It reduces the performance considerably but it will be lock free.</p> <pre><code>Public Foo { Private Carrier&lt;T&gt; { T _item } Private Carrier&lt;T&gt; _item; public T Item { get { Dim Carrier&lt;T&gt; carrier = _item; return carrier.item; } set { Dim Carrier&lt;T&gt; carrier = new Carrier&lt;T&gt;(); carrier.item = value; _item = carrier; } } </code></pre> <p>}</p> <p>This way you can ensure that you always use referenced types and your access is lock-free. Downside is that all set operations create garbage.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload