<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=""><div class=""><blockquote type="cite" class=""><blockquote type="cite" class="">I'm writing some code where I'd like multiple threads to be writing to a common dictionary object.<br class="">Is there a recommended mechanism for doing this?<br class=""></blockquote><br class="">Wrap mutexes* around dictionary accesses. If you have a lot more reads than writes, a read/write mutex will be more efficient.<br class=""></blockquote></div><div class=""><br class=""></div>Speaking of which, the CleanroomConcurrency project for Swift provides a&nbsp;ReadWriteCoordinator that provides similar functionality using a Grand Central Dispatch feature:<div class=""><br class=""></div><div class=""><div class=""><a href="https://github.com/emaloney/CleanroomConcurrency/tree/master/Code#readwritecoordinator" class="">https://github.com/emaloney/CleanroomConcurrency/tree/master/Code#readwritecoordinator</a></div><div class=""><br class=""></div><div class="">The flip side of the threadsafe dictionary is the thread-local dictionary associated with each NSThread which can be used in a Swifty fashion with:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/emaloney/CleanroomConcurrency/tree/master/Code#threadlocalvalue" class="">https://github.com/emaloney/CleanroomConcurrency/tree/master/Code#threadlocalvalue</a></div><div class=""><br class=""></div><div class="">Disclosure: This open-source code is provided courtesy of my employer, Gilt Groupe, and ships in our "Gilt on TV" app for the new Apple TV.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 10, 2015, at 12:28 PM, Jens Alfke via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><br class=""><blockquote type="cite" class="">On Dec 10, 2015, at 9:18 AM, Lane Schwartz via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:<br class=""><br class="">I'm writing some code where I'd like multiple threads to be writing to a common dictionary object.<br class="">Is there a recommended mechanism for doing this?<br class=""></blockquote><br class="">Wrap mutexes* around dictionary accesses. If you have a lot more reads than writes, a read/write mutex will be more efficient.<br class=""><br class="">Making a thread-safe dictionary class is usually not a good idea. (This is something Java learned in between JDK 1.0 and 1.2.) It adds unavoidable overhead to every single access, and doesn’t solve the higher-level synchronization problems of the code that’s using the dictionary. Instead, use synchronization primitives in your higher-level class at the appropriate points.<br class=""><br class="">—Jens<br class=""><br class="">* which I guess you’ll have to implement using C calls to pthreads, since the Swift concurrency library isn’t ready yet<br 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></div></blockquote></div><br class=""></div></div></body></html>