{"id":18669,"date":"2017-08-18T14:19:20","date_gmt":"2017-08-18T18:19:20","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=18669"},"modified":"2022-05-05T13:49:17","modified_gmt":"2022-05-05T17:49:17","slug":"concurrency-in-swift-one-possible-approach","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/08\/18\/concurrency-in-swift-one-possible-approach\/","title":{"rendered":"Concurrency in Swift: One Possible Approach"},"content":{"rendered":"<p><a href=\"https:\/\/gist.github.com\/lattner\/31ed37682ef1576b16bca1432ea9f782\">Chris Lattner<\/a> (<a href=\"https:\/\/twitter.com\/clattner_llvm\/status\/898310296183357441\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/gist.github.com\/lattner\/31ed37682ef1576b16bca1432ea9f782\"><p>We focus on task-based concurrency abstractions commonly encountered in client and server applications, particularly those that are highly event driven (e.g. responding to UI events or requests from clients). This does not attempt to be a comprehensive survey of all possible options, nor does it attempt to solve all possible problems in the space of concurrency. Instead, it outlines a single coherent design thread that can be built over the span of years to incrementally drive Swift to further greatness.<\/p><p>[&#8230;]<\/p><p>Modern Cocoa development involves a lot of asynchronous programming using closures and completion handlers, but these APIs are hard to use. This gets particularly problematic when many asynchronous operations are used, error handling is required, or control flow between asynchronous calls is non-trivial. [&#8230;] Error handling is particularly ugly, because Swift&rsquo;s natural error handling mechanism cannot be used.<\/p><p>[&#8230;]<\/p><p>Asynchrony is the next fundamental abstraction that must be tackled in Swift, because it is\nessential to programming in the real world where we are talking to other machines, to slow\ndevices (spinning disks are still a thing!), and looking to achieve concurrency between multiple\nindependent operations.  Fortunately, Swift is not the first language to face\nthese challenges: the industry as a whole has fought this dragon and settled on\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Await\">async\/await<\/a> as the right abstraction.  We propose\nadopting this proven concept outright (with a Swift spin on the syntax).  Adopting\nasync\/await will dramatically improve existing Swift code, dovetailing with existing and\nfuture approaches to concurrency.<\/p><p>The next step is to define a programmer abstraction to define and model the independent\ntasks in a program, as well as the data that is owned by those tasks.  We propose the\nintroduction of a first-class <a href=\"https:\/\/en.wikipedia.org\/wiki\/Actor_model\">actor model<\/a>, which\nprovides a way to define and reason about independent tasks who communicate between\nthemselves with asynchronous message sending.  The actor model has a deep history of\nstrong academic work and was adopted and proven in\n<a href=\"https:\/\/www.erlang.org\">Erlang<\/a> and <a href=\"http:\/\/akka.io\">Akka<\/a>, which successfully power a large\nnumber of highly scalable and reliable systems.\nWith the actor model as a baseline, we believe we can achieve data isolation by ensuring that\nmessages sent to actors do not lead to shared mutable state.<\/p><\/blockquote>\n<p>Previously: <a href=\"https:\/\/mjtsai.com\/blog\/2016\/12\/19\/swift-challenges-and-opportunity-for-language-and-compiler-research\/\">Swift: Challenges and Opportunity for Language and Compiler Research<\/a>, <a href=\"https:\/\/mjtsai.com\/blog\/2015\/07\/14\/python-3-5-async-and-await\/\">Python 3.5: async and await<\/a>, <a href=\"https:\/\/mjtsai.com\/blog\/2010\/10\/30\/c-5-0s-async\/\">C# 5.0&rsquo;s async<\/a>.<\/p>\n\n<p>Update (2017-09-11): See also: <a href=\"https:\/\/spec.fm\/podcasts\/swift-unwrapped\/84323\">Swift Unwrapped<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Chris Lattner (tweet): We focus on task-based concurrency abstractions commonly encountered in client and server applications, particularly those that are highly event driven (e.g. responding to UI events or requests from clients). This does not attempt to be a comprehensive survey of all possible options, nor does it attempt to solve all possible problems in [&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-04-22T17:23:32Z","apple_news_api_id":"aaf2aa1e-dc18-4027-90f5-8587be3a3616","apple_news_api_modified_at":"2022-05-05T17:49:27Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/AqvKqHtwYQCeQ9YWHvjo2Fg","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,1797,46,71,2200,901],"class_list":["post-18669","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-concurrency","tag-erlang-programming-language","tag-languagedesign","tag-programming","tag-swift-concurrency","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18669","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=18669"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18669\/revisions"}],"predecessor-version":[{"id":18845,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18669\/revisions\/18845"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=18669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=18669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=18669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}