gpt4 book ai didi

multithreading - Swift 有任何本地并发和多线程支持吗?

转载 作者:搜寻专家 更新时间:2023-10-31 22:14:17 24 4
gpt4 key购买 nike

我正在编写一个 Swift 客户端来与嵌入式系统上的服务器(用 C 语言编写)进行通信。它与 iOS/OSX 无关,因为我使用的是最近发布的 Ubuntu 版本。

Swift 对并发有任何原生支持吗?我知道 Apple 不鼓励开发人员使用线程并鼓励通过 GCD 将任务移交给调度队列。问题是 GCD 似乎只适用于 Darwin(而 NSThread 是 Cocoa 的一部分)。

例如,C++11 和 Java 将线程和并发作为其标准库的一部分。我知道平台特定的东西,比如 unix 上的 posix 可以在某种 C 包装器下使用,但对我来说,这首先破坏了使用 Swift 的意义(干净、易于理解的代码等)。

最佳答案

2021年来了......

从 Swift 5.5 开始,more options are available比如 async/await 编程模型和参与者。

仍然没有对线程的直接操作,这是(截至目前)的设计选择。

If you’ve written concurrent code before, you might be used to working with threads. The concurrency model in Swift is built on top of threads, but you don’t interact with them directly. An asynchronous function in Swift can give up the thread that it’s running on, which lets another asynchronous function run on that thread while the first function is blocked.

2015 年原始答案

引自Swift's GitHub ,有一个关于“进化”的自述文件:

Concurrency: Swift 3.0 relies entirely on platform concurrency primitives (libdispatch, Foundation, pthreads, etc.) for concurrency. Language support for concurrency is an often-requested and potentially high-value feature, but is too large to be in scope for Swift 3.0.

我想这意味着在可预见的 future ,不会有线程的语言级“原语”。

关于multithreading - Swift 有任何本地并发和多线程支持吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34164414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com