gpt4 book ai didi

swift - iOS 在没有失败的情况下卡在实例化依赖中

转载 作者:行者123 更新时间:2023-11-28 11:04:03 27 4
gpt4 key购买 nike

如果我有类似的东西

class Foo {
static shared = Foo()
init() {
print("init Foo")
let _ = Bar.shared
}
}

class Bar {
static shared = Bar()
init() {
print("init Bar")
let _ = Foo.shared
}
}

// somwehere else:
let _ = Foo.shared

然后应用程序卡住了。什么都没发生。我知道这个设计是错误的,但我想知道为什么应用程序没有崩溃、报告错误或至少打印一个循环。以上代码打印

init Foo
init Bar

就是这样,表明它不是循环而是卡住了。对正在发生的事情有想法吗?

最佳答案

在 Swift 中,静态类型属性以保证线程安全的方式延迟初始化。

注意 Type Properties

Stored type properties are lazily initialized on their first access. They are guaranteed to be initialized only once, even when accessed by multiple threads simultaneously, and they do not need to be marked with the lazy modifier.

这个 only once 特性利用了类似于 dispatch_once 的东西(或者完全是它本身),它需要互斥。

当初始化Foo.shared时,Foo.shared的锁被锁定。当它被锁定时,Bar.shared 需要被初始化,所以 Bar.shared 的锁被锁定。当两者都被锁定时,Foo.shared 需要初始化,但是它的锁已经被锁定,所以等待直到锁被释放......

我们称这种情况为死锁

关于swift - iOS 在没有失败的情况下卡在实例化依赖中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39312818/

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