Thread safety of method calls on “shared” static constant property

大城市里の小女人 提交于 2019-12-08 06:22:18

问题


I have a Swift class which uses a traditional Cocoa singleton pattern: one static shared constant and a private init that is only called once for that shared constant. It's like this:

public class Foo {
    public static let shared = Foo()

    private init() { /* ... */ }

    public func bar() { /* ... */ }
    public func baz() { /* ... */ }
}

// Meanwhile, in multiple places upon multiple threads:

Foo.shared.bar()

Foo.shared.baz()

If I have a dozen threads calling functions on that constant, does it pause all calls until that initializer completes, or should I have some protections within those instance functions to wait for initialization to complete?

来源:https://stackoverflow.com/questions/54189782/thread-safety-of-method-calls-on-shared-static-constant-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!