{"id":26194,"date":"2019-08-06T16:21:37","date_gmt":"2019-08-06T20:21:37","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=26194"},"modified":"2019-08-08T15:33:02","modified_gmt":"2019-08-08T19:33:02","slug":"practical-concurrency-some-rules","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2019\/08\/06\/practical-concurrency-some-rules\/","title":{"rendered":"Practical Concurrency: Some Rules"},"content":{"rendered":"<p><a href=\"https:\/\/www.markbernstein.org\/Aug19\/PracticalConcurrencySomeRu.html\">Mark Bernstein<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.markbernstein.org\/Aug19\/PracticalConcurrencySomeRu.html\">\n<p>You can get away with murder. Going by the book, you&rsquo;ve got to use extreme caution and you&rsquo;ve always got to get it right. In practice, Tinderbox Six took all sorts of risks and accepted that it was Doing It Wrong in order to get stuff done.<\/p>\n<\/blockquote>\n<p>It sounds like there&rsquo;s a good story here. My experience has been more that it&rsquo;s sometimes hard to know when you&rsquo;re doing something wrong, but that once you&rsquo;re aware of it it&rsquo;s not that much extra effort to do it by the book. I try to do that as much as possible because it&rsquo;s no fun tracking down concurrency-related bugs.<\/p>\n\n<blockquote cite=\"https:\/\/www.markbernstein.org\/Aug19\/PracticalConcurrencySomeRu.html\">\n<p>The profiler is now good. It wasn&rsquo;t always. In the Tinderbox 4 era, firing up the Profiler meant recompiling the world, and that took 20 minutes. Then, you'd get slow and inconclusive results, and throw up your hands. Life today is better[&#8230;]<\/p>\n<\/blockquote>\n<p>The profiler is indeed really great&mdash;when it works. Unfortunately, I have not had much luck getting it to work with tests. Most of the time, I Control-click on the test diamond and choose &ldquo;Profile &lsquo;testName()&rsquo;&rdquo;, it recompiles and launches Instruments, but it doesn&rsquo;t actually run the test code. Sometimes I can get it to work with the old and clunkier approach of stopping at a breakpoint and then attaching Instruments to the running process, but other times it doesn&rsquo;t actually record what it&rsquo;s supposed to.<\/p>\n\n<blockquote cite=\"https:\/\/www.markbernstein.org\/Aug19\/PracticalConcurrencySomeRu.html\">\n<p>If a queue has one clear purpose, it&rsquo;s easier to be confident it won&rsquo;t deadlock. Dispatch queues are cheap. Don&rsquo;t share queues, don&rsquo;t reuse queues, don&rsquo;t worry about making queues.<\/p>\n<p>Classes should encapsulate their queues.<\/p><\/blockquote>\n\n<p id=\"practical-concurrency-some-rules-update-2019-08-07\">Update (2019-08-07): <a href=\"https:\/\/twitter.com\/tclementdev\/status\/1158858996293541888\">Thomas Clement<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/tclementdev\/status\/1158858996293541888\">\n<p>Queues are not cheap, you should reuse queues, you should worry a lot about making (too many) queues.<\/p>\n<\/blockquote>\n<p>He&rsquo;s posted some <a href=\"https:\/\/gist.github.com\/tclementdev\/6af616354912b0347cdf6db159c37057\">libdispatch efficiency tips<\/a> and links to some good threads from Swift Evolution. You may find them easier to read in the new Swift forums:<\/p>\n\n<ul>\n<li><a href=\"https:\/\/forums.swift.org\/t\/concurrency-async-await-actors\/6516\">[Concurrency] async\/await + actors<\/a><\/li>\n<li><a href=\"https:\/\/forums.swift.org\/t\/concurrency-fixing-race-conditions-in-async-await-example\/6514\">[Concurrency] Fixing race conditions in async\/await example<\/a><\/li>\n<\/ul>\n\n<p>Although some of the messages seem to have been imported out-of-order.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2018\/04\/20\/gcd-tips\/\">GCD Tips<\/a><\/li>\n<\/ul>\n\n<p id=\"practical-concurrency-some-rules-update-2019-08-08\">Update (2019-08-08): <a href=\"https:\/\/twitter.com\/mpweiher\/status\/1159198085257580544\">Marcel Weiher<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/mpweiher\/status\/1159198085257580544\">\n<p>From a, cough, <a href=\"http:\/\/www.amazon.com\/dp\/B06X9Z79C7\/?tag=michaeltsai-20\">well-respected<\/a>, cough, <a href=\"https:\/\/blog.metaobject.com\/2017\/03\/so-i-wrote-book-about-performance.html\">performance book<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.amazon.com\/dp\/B06X9Z79C7\/?tag=michaeltsai-20\"><p>In the end, I&rsquo;ve rarely had to use  multi-threading for speeding up a CPU-bound task in anger, and chances are good that I would have made my code slower rather than faster.<\/p>\n<p>... The advice to never optimize without measuring as you go along goes double for multi-threading.<\/p><\/blockquote>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Mark Bernstein: You can get away with murder. Going by the book, you&rsquo;ve got to use extreme caution and you&rsquo;ve always got to get it right. In practice, Tinderbox Six took all sorts of risks and accepted that it was Doing It Wrong in order to get stuff done. It sounds like there&rsquo;s a good [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2019-08-06T20:21:39Z","apple_news_api_id":"7b7bf6bb-a8dd-4e21-82ea-d85774401105","apple_news_api_modified_at":"2019-08-08T19:33:11Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAw==","apple_news_api_share_url":"https:\/\/apple.news\/Ae3v2u6jdTiGC6thXdEARBQ","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[800,880,225,30,32,1609,71,268,1344,226],"class_list":["post-26194","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-concurrency","tag-grand-central-dispatch-gcd","tag-instruments","tag-mac","tag-macapp","tag-macos-10-14","tag-programming","tag-testing","tag-tinderbox","tag-xcode"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=26194"}],"version-history":[{"count":5,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26194\/revisions"}],"predecessor-version":[{"id":26218,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26194\/revisions\/26218"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=26194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=26194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=26194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}