示例工程 20-async-await-structured-concurrency

20-async-await-structured-concurrency/Diagnostics/CaptureMutation.swift

swift · 0.4 KB

// 故意编译失败:并发闭包里修改捕获的可变变量。
// 复现:swiftc -swift-version 6 Diagnostics/CaptureMutation.swift
func sumConcurrently() async -> Int {
    var total = 0
    await withTaskGroup(of: Void.self) { group in
        for value in 1...3 {
            group.addTask {
                total += value
            }
        }
    }
    return total
}