Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Catch Up Actor & Sendable

Catch Up Actor & Sendable

社内のiOS勉強会の発表資料「Catch Up Actor & Sendable」

Toshiyana

May 17, 2024
Tweet

More Decks by Toshiyana

Other Decks in Programming

Transcript

  1. Actor Type • • • • • • • 特徴

    参照型 プロパティへのアクセスが同期的に行われることが基本的にコンパイル時に保証 他の型(enum, sturct, class)と性質は似ている property, method, initializer, subscript, ... をもてる protocol に適合可能 extensionで拡張可能 継承は非サポート
  2. Actorがデータ競合を防ぐ仕組み • • Actorインスタンスのメンバ(stored/computed property, method, subscript 等)は、デフォルト では「actor-isolated」 な定義として扱われる

    actor-isolatedな定義は、Actorインスタンス内部(self)からのみ直接参照でき、外部から直接参照 しようとするとコンパイルエラーになる ← 可変データが保護されデータ競合を防ぐ
  3. 補足: Actorの内部処理 > 各アクター・インスタンスには独自のシリアル・エグゼキューター(Structured Concurrency プ ロポーザルで記述)が含まれます。デフォルトのシリアル・エクゼキュータは、部分タスクを一度 に実行します。これは概念的にはシリアルの DispatchQueue に似ていますが、重要な違いがあり

    ます:アクタを待っているタスクは、元々そのアクタを待っていたのと同じ順序で実行されること は保証されていません。Swift のランタイムシステムは、優先順位エスカレーションのような技術を 使用して、可能な限り優先順位の逆転を避けることを目的としています。したがって、ランタイム システムは、そのキューからアクタ上で実行する次のタスクを選択するときに、タスクの優先順位 を考慮します。これは、厳密に先入れ先出しのシリアル DispatchQueue とは対照的です。さらに、 Swift のアクターランタイムは、Swift の非同期関数を最大限に活用するために、Dispatch よりも軽 量なキューの実装を使用します。 https://github.com/apple/swift-evolution/blob/main/proposals/0306-actors.md#actor-isolation
  4. Sendableとは • • • • • • • Sendableとは: コピーすることで、ある並行性ドメインから別の並行性ドメインに安全に渡せるも

    の Sendableになるもの 値型 アクター型 不変クラス 内部で同期的に処理されるクラス @Sendableでマークされた関数,クロージャー
  5. Reference • • • • • • • • https://developer.apple.com/documentation/swift/sendable

    https://developer.apple.com/documentation/swift/actor https://github.com/apple/swift-evolution/blob/main/proposals/0306-actors.md https://github.com/apple/swift-evolution/blob/main/proposals/0302-concurrent-value-and- concurrent-closures.md https://docs.swift.org/swift-book/documentation/the-swift-programming- language/concurrency/ https://developer.apple.com/videos/play/wwdc2021/10133/ https://qiita.com/maiyama18/items/c815f13f17d8e4095f68 https://medium.com/@Lakshmnaidu/class-vs-struct-vs-actor-a105eb21fdae