1

従来の Cocoa シングルトン パターンを使用する Swift クラスがあります。1 つの静的定数と、その共有定数に対して 1 回だけ呼び出されるsharedプライベートです。initこんな感じです:

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()

その定数で関数を呼び出す多数のスレッドがある場合、そのイニシャライザが完了するまですべての呼び出しを一時停止しますか、それとも初期化が完了するまで待機するためにそれらのインスタンス関数内でいくつかの保護を行う必要がありますか?

4

0 に答える 0