<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"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></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">) ->
</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>)
{ () -> <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'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<Int, Int>:</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'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 'get' and 'set' operations are atomic, but the whole sequence isn't. The results of operations that other threads execute during "tmp += 1" will be overwritten by the following 'subscript_set'.</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<i;j++){if(!(i%j)){j=0;break;}}if<br>(j){printf("%d\n",i);}}} /*Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>>*/</div>
</div></div>