actor 是线程安全的么?

actor 是线程安全的么?

The Actor Reentrancy Problem in Swift

不是的。考虑reentrance的情况

suspension point是否在actor里,如果有的话,那么在并发的两个task,就有可能会发生重入(Reentrance)的情况。

举个例子:

swift 复制代码
actor BankAccount {
    
    private var balance = 1000
    
    func withdraw(_ amount: Int) async {
        
        print("🤓 Check balance for withdrawal: (amount)")
        
        guard canWithdraw(amount) else {
            print("🚫 Not enough balance to withdraw: (amount)")
            return
        }
        
        guard await authorizeTransaction() else {
            return
        }
        
        print("✅ Transaction authorized: (amount)")
        
        balance -= amount
        
        print("💰 Account balance: (balance)")
    }
    
    private func canWithdraw(_ amount: Int) -> Bool {
        return amount <= balance
    }
    
    private func authorizeTransaction() async -> Bool {
        
        // Wait for 1 second
        try? await Task.sleep(nanoseconds: 1 * 1000000000)
        
        return true
    }
}
​
let account = BankAccount()
​
Task {
    await account.withdraw(800)
}
​
Task {
    await account.withdraw(500)
}

我们可以想象一下,这个会输出什么结果?3,2,1

乍一看,我们会想,先执行完await account.withdraw(800),然后剩余200,然后执行await account.withdraw(500),发现余额不足,不执行。

但是结果如下:

yaml 复制代码
🤓 Check balance for withdrawal: 800
🤓 Check balance for withdrawal: 500
✅ Transaction authorized: 800
💰 Account balance: 200
✅ Transaction authorized: 500
💰 Account balance: -300

你看,余额变成了-300,这明显与我们的认知不符合。那么为什么会出现的结果呢,其实是因为,actor只是能保证不出现数据竞争,但是我们的withdraw方法都会在authorizeTransaction挂起,那么也就是意味着,我们并没有在一个task执行完,才去执行另一个。也就是说,它并不会去判断挂起的时候,可变的状态还是保持你刚进入task的状态,可能有点拗口,那拿这个例子举例,就是actor不会保证,balance会自动修改为task1执行完后的值,也就是200。所以我们在做balance -= amount操作的时候,需要再判断一下,余额是否满足条件,这也是apple建议我们做的,即始终保持actor的状态变更是同步的。

即:

swift 复制代码
func withdraw(_ amount: Int) async {
    
    // Perform authorization before check balance
    guard await authorizeTransaction() else {
        return
    }
    print("✅ Transaction authorized: (amount)")
    
    print("🤓 Check balance for withdrawal: (amount)")
    guard canWithdraw(amount) else {
        print("🚫 Not enough balance to withdraw: (amount)")
        return
    }
    
    balance -= amount
    
    print("💰 Account balance: (balance)")
    
}

但是,假设authorizeTransaction方法非常耗时,而余额很明显是不满足扣款条件的,就是一种资源的浪费。所以我们可以:

swift 复制代码
func withdraw(_ amount: Int) async {
    
    print("🤓 Check balance for withdrawal: (amount)")
    guard canWithdraw(amount) else {
        print("🚫 Not enough balance to withdraw: (amount)")
        return
    }
    
    guard await authorizeTransaction() else {
        return
    }
    print("✅ Transaction authorized: (amount)")
    
    // Check balance again after the authorization process
    guard canWithdraw(amount) else {
        print("⛔️ Not enough balance to withdraw: (amount) (authorized)")
        return
    }
​
    balance -= amount
    
    print("💰 Account balance: (balance)")
    
}

虽然多做了一次canWithdraw,但是可以提升效率,且避免了重入的问题。

相关推荐
幸福回头19 小时前
ms-swift 代码推理数据集
llm·swift
不二狗1 天前
每日算法 -【Swift 算法】Two Sum 问题:从暴力解法到最优解法的演进
开发语言·算法·swift
struggle20253 天前
适用于 iOS 的 开源Ultralytics YOLO:应用程序和 Swift 软件包,用于在您自己的 iOS 应用程序中运行 YOLO
yolo·ios·开源·app·swift
一丝晨光4 天前
数值溢出保护?数值溢出应该是多少?Swift如何让整数计算溢出不抛出异常?类型最大值和最小值?
java·javascript·c++·rust·go·c·swift
Swift社区4 天前
Swift实战:如何优雅地从二叉搜索树中挑出最接近的K个值
开发语言·ios·swift
fydw_7154 天前
大语言模型RLHF训练框架全景解析:OpenRLHF、verl、LLaMA-Factory与SWIFT深度对比
语言模型·swift·llama
文件夹__iOS4 天前
深入浅出 iOS 对象模型:isa 指针 与 Swift Metadata
ios·swift
I烟雨云渊T6 天前
iOS实名认证模块的具体实现过程(swift)
ios·cocoa·swift
Swift社区7 天前
LeetCode 270:在二叉搜索树中寻找最接近的值(Swift 实战解析)
算法·leetcode·swift
I烟雨云渊T7 天前
iOS瀑布流布局的实现(swift)
开发语言·ios·swift