<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
But isn’t that really a problem with that use case rather than the concurrent dictionary itself? There are a lot of bad things one can do with almost any code. For example, one can naively use the standard library dictionary in subtle, unsafe ways with concurrency
 and get into trouble.
<div class=""><br class="">
</div>
<div class="">Of course “&#43;-&quot; would be a bad idea to do in this context. Indeed, I am using this concurrent dictionary in a special use case, and I would never do the “&#43;=“ operation as you suggest. It’s not even relevant in my code. I am just using this concurrent
 dictionary to keep track of concurrent events being completed and posted from different threads. When the event completes it gets put into the dictionary with the value being the immutable result. Alternatively, I could have put the concurrency code outside
 of the dictionary, but in my case that made for awkward access and opened the possibility of accidentally referencing the dictionary directly in an unsafe way. The safest option for me was to create the concurrent dictionary and the subscript allowed the code
 to be easier to read and write.<br class="">
<div apple-content-edited="true" class="">
<div style="color: rgb(0, 0, 0); font-family: Helvetica;  font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
<div class="">
<div class=""><font class="Apple-style-span" size="3"><span class="Apple-style-span" style="font-size: 11px;">_____________________________________________________________________________________</span></font></div>
<div class=""><font class="Apple-style-span" size="3"><span class="Apple-style-span" style="font-size: 12px;"><b class=""><font class="Apple-style-span" color="#935201">Thomas Pelaia II, Ph.D.</font></b></span></font>&nbsp;&nbsp;| Applications Leader, Accelerator Physics,
 Research Accelerator Division</div>
<div class="">Spallation Neutron Source &nbsp;| &nbsp;Oak Ridge National Lab, Building 8600, MS-6462, Oak Ridge, TN 37831</div>
<div class="">phone:&nbsp;<b class="">(865) 414-7960</b>&nbsp; | FaceTime: <a href="mailto:t6p@ornl.gov" class="">
t6p@ornl.gov</a> | &nbsp;fax: (865) 574-6617 &nbsp;| &nbsp;homepage:&nbsp;<font class="Apple-style-span" size="3"><span class="Apple-style-span" style="font-size: 12px;"><b class=""><a href="http://www.ornl.gov/~t6p" class="">http://www.ornl.gov/~t6p</a></b></span></font></div>
</div>
</div>
</div>
<br class="">
<div>
<blockquote type="cite" class="">
<div class="">On Dec 10, 2015, at 12:44 PM, Dmitri Gribenko &lt;<a href="mailto:gribozavr@gmail.com" class="">gribozavr@gmail.com</a>&gt; wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div dir="ltr" class="">
<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" class="">&lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt;</span> wrote:<br class="">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word" class="">
<div class="">
<div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class="">
/* provide subscript accessors */</div>
<div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)" class="">
<span style="" class=""><span style="white-space:pre-wrap" class=""></span></span><span style="color:#bb2ca2" class="">subscript</span><span style="" class="">(key:
</span>KeyType<span style="" class="">) -&gt; </span>ValueType<span style="" class="">? {</span></div>
<div style="margin:0px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class=""></span><span style="color:#bb2ca2" class="">get</span> {</div>
<div style="margin:0px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class=""></span><span style="color:#bb2ca2" class="">var</span> value :
<span style="color:#703daa" class="">ValueType</span>?</div>
<div style="margin:0px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class=""></span><span style="color:#3d1d81" class="">dispatch_sync</span>(<span style="color:#bb2ca2" class="">self</span>.<span style="color:#4f8187" class="">queue</span>)
 { () -&gt; <span style="color:#703daa" class="">Void</span> <span style="color:#bb2ca2" class="">
in</span></div>
<div style="margin:0px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class=""></span>value =
<span style="color:#bb2ca2" class="">self</span>.<span style="color:#4f8187" class="">internalDictionary</span>[key]</div>
<div style="margin:0px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class=""></span>}</div>
<div style="margin:0px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class=""></span><span style="color:#bb2ca2" class="">return</span> value</div>
<div style="margin:0px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class=""></span>}</div>
<div style="margin:0px;line-height:normal;font-family:Menlo;min-height:16px" class="">
<br class="">
</div>
<div style="margin:0px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class=""></span><span style="color:#bb2ca2" class="">set</span> {</div>
<div style="margin:0px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class=""></span><span style="color:#31595d" class="">setValue</span>(newValue, forKey: key)</div>
<div style="margin:0px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class=""></span>}</div>
<div style="margin:0px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class=""></span>}</div>
</div>
</div>
</blockquote>
</div>
<div class="gmail_extra"><br class="">
</div>
Please don't do this, unless you have a very special use case.&nbsp; This is inherently racy on high level, even though it is safe in the language.&nbsp; Consider two threads operating on a shared ConcurrentDictionary&lt;Int, Int&gt;:</div>
<div class="gmail_extra"><br class="">
</div>
<div class="gmail_extra">d[42] &#43;= 1</div>
<div class="gmail_extra"><br class="">
</div>
<div class="gmail_extra">Here's what the code compiles into:</div>
<div class="gmail_extra"><br class="">
</div>
<div class="gmail_extra">var tmp = d.subscript_get(42)<br class="">
<div class="">tmp &#43;= 1</div>
<div class="">d.subscript_set(42, tmp)</div>
<div class=""><br class="">
</div>
<div class="">The 'get' and 'set' operations are atomic, but the whole sequence isn't.&nbsp; The results of operations that other threads execute during &quot;tmp &#43;= 1&quot; will be overwritten by the following 'subscript_set'.</div>
<div class=""><br class="">
</div>
<div class="">Dmitri</div>
<div class=""><br class="">
</div>
-- <br class="">
<div class="gmail_signature">main(i,j){for(i=2;;i&#43;&#43;){for(j=2;j&lt;i;j&#43;&#43;){if(!(i%j)){j=0;break;}}if<br class="">
(j){printf(&quot;%d\n&quot;,i);}}} /*Dmitri Gribenko &lt;<a href="mailto:gribozavr@gmail.com" target="_blank" class="">gribozavr@gmail.com</a>&gt;*/</div>
</div>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>