Swift意欲成爲無數據競爭的併發編程語言

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Swift團隊發佈了改進Swift併發支持的路線圖。在第一個階段,Swift將提供async語法和actor,而在第二個階段,重點是消除數據競爭和死鎖。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Swift併發編程模型的核心不會改變,它保留了在獨立隊列上分派無鎖操作並通過回調返回結果的基本理念。這種方法有它的缺點,如編碼開銷、缺乏效率和容易出錯,程序員必須確保嚴格遵循正確的模式,以保證正確性和性能。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"模式是好的,但用Swift來表達會丟掉重要的結構,並導致出現問題。解決方案是將這些模式引入到語言中。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以協程爲基礎,Swift的"},{"type":"link","attrs":{"href":"https:\/\/github.com\/DougGregor\/swift-evolution\/blob\/async-await\/proposals\/nnnn-async-await.md","title":"","type":null},"content":[{"type":"text","text":"async\/await語法"}]},{"type":"text","text":"看起來會像下面這樣:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"plain"},"content":[{"type":"text","text":"internal func asyncMethod() async {\n result = await anotherClass.anotherAsyncMethod()\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這種語法的一個好處是消除了在閉包中捕獲self時需要進行顯式聲明的要求。Swift async\/await語法的一個特殊特徵是await關鍵字只能在包含異步代碼的表達式開始時指定一次,而且不能在每個async函數之前重複使用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除了async和await之外,Swift還將引入了"},{"type":"link","attrs":{"href":"https:\/\/forums.swift.org\/t\/concurrency-actors-actor-isolation\/41613","title":"","type":null},"content":[{"type":"text","text":"actor類型"}]},{"type":"text","text":"。這是定義類的一種簡單的方式,這種類擁有私有隊列,用於通過序列化的方式同步對其內部狀態的訪問。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"plain"},"content":[{"type":"text","text":"actor class AnActorClass {\n \/\/ 某些狀態\n func modifyState() async { ... }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在使用上面的代碼時,編譯器將確保所有async方法都在actor的私有隊列上運行,並對直接訪問其狀態的操作進行標記。這將帶來很多優化性能的可能性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Swift還將支持一種特定的語法,將類的async方法綁定到特定的全局actor。用於同步UI操作的UIActor就適用這種語法,因爲這種操作必鬚髮生在主隊列上。例如,你可以這樣:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"plain"},"content":[{"type":"text","text":"@UIActor\nclass AClassDoingUIStuff {\n func doSomeUITask() async { ... }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在第二階段,Swift團隊將添加完全的actor隔離,以防止數據競爭和死鎖。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"actor隔離的基本思想與獨佔訪問內存的思想類似,並以此爲基礎。Swift的併發設計從actor的自然隔離開始,使用所有權作爲補充工具,提供一種易於使用和可組合的安全併發方法。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"默認情況下,actor將提供基本的隔離。這意味着它們將保護自己的內部狀態不受數據競爭的影響,比如屬性、let常量、本地值,等等。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是,當涉及到使用UnsafeMutablePointer引用的內存、全局內存和類引用時,actor自身無法防止數據競爭,因爲它們可以在actor之間傳遞,因此也可能會造成數據競爭。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"即使只有基本的actor模型,開發人員也可以通過全局actor來減少數據競爭:如果一個框架需要在一個特定隊列上執行所有操作,它可以定義一個全局actor,類似於UIActor,並在協議中指定它。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不過,爲了獲得完全的actor隔離,Swift需要添加新的特性,其中最重要的是將類型聲明爲“actor local”。這樣就不可能在actor之間傳遞actor類型。如果真的需要這麼做,可以在傳遞actor之前克隆或取消共享。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"到了某個時間點,將默認執行這些新特性,這意味着所有的類都將是“actor local”的,而全局變量將被要求通過全局actor或被聲明爲“actor unsafe”來獲得保護。在將併發安全加到語言併發機制的核心的同時,這也意味着向後源代碼兼容性被破壞,儘管Swift團隊希望這種影響不會太大。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲複雜性的存在,要讓Swift成爲一種無數據競爭的併發語言需要大量的語言擴展,需要跨越多個版本才能實現,而關於actor完全隔離的很多細節仍有待最終確定。隨着新細節的不斷髮布,InfoQ將繼續跟蹤報道。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"原文鏈接"},{"type":"text","text":":"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/www.infoq.com\/news\/2020\/11\/swift-concurrency-roadmap\/","title":"","type":null},"content":[{"type":"text","text":"Swift Aims to Become a Data Race-Free Concurrent Language"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章