<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
I have one that I created for Swift on OS X which uses libdispatch to protect access to the underlying data:
<div class=""><br class="">
</div>
<div class="">
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">
/* dictionary that allows thread safe concurrent access */</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">final</span>
<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
class</span> ConcurrentDictionary<KeyType:Hashable,ValueType> : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
NSObject</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
SequenceType</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
DictionaryLiteralConvertible</span> {</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>/* internal dictionary */</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">private</span>
<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
var</span> internalDictionary : [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">KeyType</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">ValueType</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; color: rgb(0, 132, 0);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>/* queue modfications using a barrier and allow concurrent read operations */</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">private</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">
</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> queue =
</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">dispatch_queue_create</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(
</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"dictionary access"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">,
</span>DISPATCH_QUEUE_CONCURRENT<span style="font-variant-ligatures: no-common-ligatures; color: #000000" 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; min-height: 16px;" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>/* count of key-value pairs in this dicitionary */</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> count :
<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
Int</span> {</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> count =
<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">
0</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">dispatch_sync</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">queue</span>)
{ () -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
Void</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
in</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span>count =
</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>internalDictionary<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">count</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> count</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></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; min-height: 16px;" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>// safely get or set a copy of the internal dictionary value</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> dictionary : [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">KeyType</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">ValueType</span>]
{</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">get</span> {</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> dictionaryCopy : [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">KeyType</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">ValueType</span>]?</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">dispatch_sync</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">queue</span>)
{ () -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
Void</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
in</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span>dictionaryCopy =
<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">dictionary</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> dictionaryCopy!</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></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 class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">set</span> {</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">
dictionaryCopy = newValue<span class="Apple-tab-span" style="white-space:pre"> </span>
</span>// create a local copy on the current thread</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">dispatch_async</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">queue</span>)
{ () -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
Void</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
in</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">internalDictionary</span>
= dictionaryCopy</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></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; min-height: 16px;" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>/* initialize an empty dictionary */</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>override<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">
</span>convenience<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">
</span>init<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">() {</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(
dictionary: [KeyType:ValueType]() )</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></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; min-height: 16px;" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>/* allow a concurrent dictionary to be initialized using a dictionary literal of form: [key1:value1, key2:value2,
...] */</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">convenience</span>
<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
required</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
init</span>(dictionaryLiteral elements: (<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">KeyType</span>,
<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
ValueType</span>)...) {</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">
dictionary = </span>Dictionary<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span>KeyType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">,</span>ValueType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" 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 class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> (key,value)
<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
in</span> elements {</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span>dictionary[key] = value</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></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 class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(dictionary:
dictionary)</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></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; min-height: 16px;" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>/* initialize a concurrent dictionary from a copy of a standard dictionary */</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>( dictionary: [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">KeyType</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">ValueType</span>]
) {</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>internalDictionary<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">
= dictionary</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></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; min-height: 16px;" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>/* provide subscript accessors */</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">subscript</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(key:
</span>KeyType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) ->
</span>ValueType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">? {</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">get</span> {</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> value :
<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
ValueType</span>?</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">dispatch_sync</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">queue</span>)
{ () -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
Void</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
in</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span>value =
<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">internalDictionary</span>[key]</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> value</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></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 class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">set</span> {</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">setValue</span>(newValue, forKey: key)</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></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; min-height: 16px;" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>/* assign the specified value to the specified key */</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> setValue(value:
<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
ValueType</span>?, forKey key: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
KeyType</span>) {</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>// need to synchronize writes for consistent modifications</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>dispatch_barrier_async<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">queue</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)
{ () -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Void</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">
</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>internalDictionary<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">[key]
= value</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></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; min-height: 16px;" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>/* remove the value associated with the specified key and return its value if any */</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> removeValueForKey(key:
<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
KeyType</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
ValueType</span>? {</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> oldValue :
<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
ValueType</span>? = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
nil</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>// need to synchronize removal for consistent modifications</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>dispatch_barrier_sync<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">queue</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)
{ () -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Void</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">
</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span>oldValue =
<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">internalDictionary</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">removeValueForKey</span>(key)</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> oldValue</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></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; min-height: 16px;" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span>/* Generator of key-value pairs suitable for for-in loops */</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">
generate() -> </span>Dictionary<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span>KeyType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">,</span>ValueType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>.</span>Generator<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">
{</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre"></span></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">
generator : </span>Dictionary<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span>KeyType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">,</span>ValueType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>.</span>Generator<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">!</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">dispatch_sync</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">queue</span>)
{ () -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
Void</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
in</span></div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span>generator =
<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">internalDictionary</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">generate</span>()</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> generator</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div style="margin: 0px; line-height: normal; font-family: Menlo;" class="">}</div>
</div>
<div class=""><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> | Applications Leader, Accelerator Physics,
Research Accelerator Division</div>
<div class="">Spallation Neutron Source | Oak Ridge National Lab, Building 8600, MS-6462, Oak Ridge, TN 37831</div>
<div class="">phone: <b class="">(865) 414-7960</b> | FaceTime: <a href="mailto:t6p@ornl.gov" class="">
t6p@ornl.gov</a> | fax: (865) 574-6617 | homepage: <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:18 PM, swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div dir="ltr" class="">Hi,
<div class=""><br class="">
</div>
<div class="">I'm writing some code where I'd like multiple threads to be writing to a common dictionary object.</div>
<div class=""><br class="">
</div>
<div class="">Is there a recommended mechanism for doing this?</div>
<div class=""><br class="">
</div>
<div class="">Thanks,</div>
<div class="">Lane</div>
<div class=""><br class="">
</div>
</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=hrXL0N6gfYDEoAiG1qg-2B3R-2F92FHBTMgYM00Phymd9OQXbXAtIRhDvv2je0otHAO-2FHvjE5iCh3FIP1a7h9euElZGe1-2FmX1YC4nhnUxhBGtHoWZRxCRkPcdW47Gz-2BHKsv-2Fz-2FlK6nqk9BzjdHsZyGC4CIcxX5k0xlo5o7tP-2BRd39TC8wvLdWhfsRzka8Ba0kZjTWIyyOL6av7FmIP2oDN2aqSTgPN4Z7-2BLOAw7gjG9FjZU-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
_______________________________________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
https://lists.swift.org/mailman/listinfo/swift-users<br class="">
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>