<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Dec 10, 2015 at 9:24 AM, Pelaia II, Tom via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">



<div style="word-wrap:break-word"><div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">/* provide subscript accessors */</div>
<div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)">
<span style="color:#000000"><span style="white-space:pre-wrap"></span></span><span style="color:#bb2ca2">subscript</span><span style="color:#000000">(key:
</span>KeyType<span style="color:#000000">) -&gt;
</span>ValueType<span style="color:#000000">? {</span></div>
<div style="margin:0px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap"></span><span style="color:#bb2ca2">get</span> {</div>
<div style="margin:0px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap"></span><span style="color:#bb2ca2">var</span> value :
<span style="color:#703daa">
ValueType</span>?</div>
<div style="margin:0px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap"></span><span style="color:#3d1d81">dispatch_sync</span>(<span style="color:#bb2ca2">self</span>.<span style="color:#4f8187">queue</span>)
 { () -&gt; <span style="color:#703daa">
Void</span> <span style="color:#bb2ca2">
in</span></div>
<div style="margin:0px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap"></span>value =
<span style="color:#bb2ca2">
self</span>.<span style="color:#4f8187">internalDictionary</span>[key]</div>
<div style="margin:0px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap"></span>}</div>
<div style="margin:0px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap"></span><span style="color:#bb2ca2">return</span> value</div>
<div style="margin:0px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap"></span>}</div>
<div style="margin:0px;line-height:normal;font-family:Menlo;min-height:16px">
<br>
</div>
<div style="margin:0px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap"></span><span style="color:#bb2ca2">set</span> {</div>
<div style="margin:0px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap"></span><span style="color:#31595d">setValue</span>(newValue, forKey: key)</div>
<div style="margin:0px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap"></span>}</div>
<div style="margin:0px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap"></span>}</div></div></div></blockquote></div><div class="gmail_extra"><br></div>Please don&#39;t do this, unless you have a very special use case.  This is inherently racy on high level, even though it is safe in the language.  Consider two threads operating on a shared ConcurrentDictionary&lt;Int, Int&gt;:</div><div class="gmail_extra"><br></div><div class="gmail_extra">d[42] += 1</div><div class="gmail_extra"><br></div><div class="gmail_extra">Here&#39;s what the code compiles into:</div><div class="gmail_extra"><br></div><div class="gmail_extra">var tmp = d.subscript_get(42)<br><div>tmp += 1</div><div>d.subscript_set(42, tmp)</div><div><br></div><div>The &#39;get&#39; and &#39;set&#39; operations are atomic, but the whole sequence isn&#39;t.  The results of operations that other threads execute during &quot;tmp += 1&quot; will be overwritten by the following &#39;subscript_set&#39;.</div><div><br></div><div>Dmitri</div><div><br></div>-- <br><div class="gmail_signature">main(i,j){for(i=2;;i++){for(j=2;j&lt;i;j++){if(!(i%j)){j=0;break;}}if<br>(j){printf(&quot;%d\n&quot;,i);}}} /*Dmitri Gribenko &lt;<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>&gt;*/</div>
</div></div>