{"id":10717,"date":"2015-02-06T17:17:04","date_gmt":"2015-02-06T22:17:04","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=10717"},"modified":"2017-10-30T15:43:11","modified_gmt":"2017-10-30T19:43:11","slug":"locks-thread-safety-and-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/02\/06\/locks-thread-safety-and-swift\/","title":{"rendered":"Locks, Thread Safety, and Swift"},"content":{"rendered":"<p><a href=\"https:\/\/www.mikeash.com\/pyblog\/friday-qa-2015-02-06-locks-thread-safety-and-swift.html\">Mike Ash<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.mikeash.com\/pyblog\/friday-qa-2015-02-06-locks-thread-safety-and-swift.html\"><p>Swift requires variables to be initialized before they&rsquo;re used. <code>pthread_mutex_init<\/code> doesn&rsquo;t use the value of the variable passed in, it just overwrites it, but Swift doesn&rsquo;t know that and so it produces an error. To satisfy the compiler, the variable needs to be initialized with something, but that&rsquo;s harder than it looks.<\/p><p>[&#8230;]<\/p><p>Getting data out of the block is a bit less pleasant, unfortunately. While <code>@synchronized<\/code> lets you <code>return<\/code> from within, that doesn&rsquo;t work with <code>with<\/code>. Instead, you have to use a <code>var<\/code> and assign to it within the block[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>Swift has no language facilities for thread synchronization, but this deficiency is more than made up for the wealth of locking APIs available in Apple&rsquo;s frameworks. GCD and <code>dispatch_queue_t<\/code> are still a masterpiece and the API works great in Swift.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Mike Ash: Swift requires variables to be initialized before they&rsquo;re used. pthread_mutex_init doesn&rsquo;t use the value of the variable passed in, it just overwrites it, but Swift doesn&rsquo;t know that and so it produces an error. To satisfy the compiler, the variable needs to be initialized with something, but that&rsquo;s harder than it looks.[&#8230;]Getting data [&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":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","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,46,903,71,901],"class_list":["post-10717","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-concurrency","tag-grand-central-dispatch-gcd","tag-languagedesign","tag-mac-os-x-10-10-yosemite","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10717","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=10717"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10717\/revisions"}],"predecessor-version":[{"id":10718,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10717\/revisions\/10718"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=10717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=10717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=10717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}