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}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章