{"id":1347,"date":"2006-10-31T12:34:14","date_gmt":"2006-10-31T16:34:14","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/2006\/10\/31\/synchronized\/"},"modified":"2006-11-07T23:56:45","modified_gmt":"2006-11-08T03:56:45","slug":"synchronized","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2006\/10\/31\/synchronized\/","title":{"rendered":"@synchronized"},"content":{"rendered":"<p>\r\n<a href=\"http:\/\/googlemac.blogspot.com\/2006\/10\/synchronized-swimming.html\">Dave MacLachlan<\/a>:\r\n<\/p>\r\n\r\n<blockquote cite=\"http:\/\/googlemac.blogspot.com\/2006\/10\/synchronized-swimming.html\">\r\n<p>\r\nBy examining the <a href=\"http:\/\/www.opensource.apple.com\/darwinsource\/10.4.7.ppc\/objc4-267.1\/runtime\/objc-sync.m\">code (ADC registration required)<\/a> that implements <code>objc_sync_enter<\/code> and <code>obc_sync_exit<\/code>, we can see that on every <code>@synchronized(foo)<\/code> block, we are actually paying for 3 lock\/unlock sequences. <code>objc_sync_enter<\/code> calls <code>id2data<\/code>, which is responsible for getting the lock associated with <code>foo<\/code>, and then locks it. <code>objc_sync_exit<\/code> also calls <code>id2data<\/code> to get the lock associated with <code>foo<\/code>, and then unlocks it. And, <code>id2data<\/code> must lock\/unlock its own internal data structures so that it can safely get the lock associated with <code>foo<\/code>, so we pay for that on each call as well.\r\n<\/p>\r\n<\/blockquote>\r\n\r\n<p>\r\nPlus the overhead of entering an exception handling block, which is currently expensive in Objective-C.\r\n<\/p>\r\n\r\n<p>Update: <a href=\"http:\/\/googlemac.blogspot.com\/2006\/11\/synchronized-swimming-part-2.html\">two faster versions<\/a>. The first takes advantage of a guarantee that (Objective-)C++ makes, and the second uses swizzling to replace the method with one that isn&rsquo;t synchronized once the initial race condition is over.<\/p>","protected":false},"excerpt":{"rendered":"<p>Dave MacLachlan: By examining the code (ADC registration required) that implements objc_sync_enter and obc_sync_exit, we can see that on every @synchronized(foo) block, we are actually paying for 3 lock\/unlock sequences. objc_sync_enter calls id2data, which is responsible for getting the lock associated with foo, and then locks it. objc_sync_exit also calls id2data to get the lock [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","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":[],"class_list":["post-1347","post","type-post","status-publish","format-standard","hentry","category-programming-category"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/1347","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=1347"}],"version-history":[{"count":0,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/1347\/revisions"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=1347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=1347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=1347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}