{"id":11203,"date":"2015-05-12T10:43:39","date_gmt":"2015-05-12T14:43:39","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=11203"},"modified":"2015-05-12T10:43:39","modified_gmt":"2015-05-12T14:43:39","slug":"using-atomics-to-get-rid-of-synchronized","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/05\/12\/using-atomics-to-get-rid-of-synchronized\/","title":{"rendered":"Using Atomics to Get Rid of @synchronized"},"content":{"rendered":"<p><a href=\"http:\/\/biasedbit.com\/blog\/objc-atomics\/\">Bruno de Carvalho<\/a> (<a href=\"https:\/\/twitter.com\/biasedbit\/status\/595627500513918977\">tweet<\/a>, <a href=\"https:\/\/news.ycombinator.com\/item?id=9493422\">comments<\/a>):<\/p>\n<blockquote cite=\"http:\/\/biasedbit.com\/blog\/objc-atomics\/\">  <p>This post talks about the use of OS low level atomic functions (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Linearizability#Compare-and-swap\"><em>compare-and-swap<\/em><\/a>, <a href=\"http:\/\/en.wikipedia.org\/wiki\/Linearizability#Fetch-and-increment\"><em>fetch-and-increment<\/em><\/a>) to achieve both <a href=\"http:\/\/en.wikipedia.org\/wiki\/Linearizability#Locking\">wait and lock free<\/a>, thread-safe concurrent algorithms in your iOS\/OSX apps.<\/p>\n<p>It introduces a few classes that abstract away the gory details of some of the <a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/System\/Conceptual\/ManPages_iPhoneOS\/man3\/atomic.3.html#\/\/apple_ref\/doc\/man\/3\/atomic\">atomic functions<\/a> made available by <a href=\"https:\/\/developer.apple.com\/library\/mac\/documentation\/System\/Reference\/OSAtomic_header_reference\/\"><code>libkern\/OSAtomic.h<\/code><\/a> into conceptually simpler components that use <code>Foundation<\/code> types.<\/p>\n<p>[&#8230;]<\/p>\n<p>While I&rsquo;ve found plenty of use cases for atomic integers and booleans, references are something that I&rsquo;ve rarely ever needed. I did come across one interesting usage for it recently.<\/p>\n<p>[&#8230;]<\/p>\n<p>If you&rsquo;re looking to extract that extra bit of performance, instead of using <code>AtomicInteger<\/code> or <code>AtomicBoolean<\/code> go straight for <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/atomic\/atomic\">std::atomic&lt;&gt;<\/a> since <code>objc_msgSend()<\/code> &mdash; calling methods on classes &mdash; has a non-neglectable impact. Just make sure that when you do so, your code remains readable and self-explanatory.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Bruno de Carvalho (tweet, comments): This post talks about the use of OS low level atomic functions (compare-and-swap, fetch-and-increment) to achieve both wait and lock free, thread-safe concurrent algorithms in your iOS\/OSX apps. It introduces a few classes that abstract away the gory details of some of the atomic functions made available by libkern\/OSAtomic.h into [&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":[55,800,31,30,571,54,74,138,71,1193],"class_list":["post-11203","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-arc","tag-concurrency","tag-ios","tag-mac","tag-memory-management","tag-objective-c","tag-opensource","tag-optimization","tag-programming","tag-standard-template-library-stl"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11203","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=11203"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11203\/revisions"}],"predecessor-version":[{"id":11204,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11203\/revisions\/11204"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=11203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=11203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=11203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}