Search
🌅

Actor의 역할

1.
코드 생성 단계
actor내에서 선언된 함수에 async를 자동으로 적용, actor의 구현에 필요한 코드를 자동으로 생성
2.
컴파일 단계
동시성 문제가 발생할 수 있는 코드를 작성하면 컴파일러가 오류를 일으켜서 수정하도록 해 줌
3.
실행 단계
각 코드가 실제로 순서대로 실행될 수 있도록 해 줌
/// Common protocol to which all actors conform. /// /// The `Actor` protocol generalizes over all actor types. Actor types /// implicitly conform to this protocol. @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) public protocol Actor : AnyObject, Sendable { /// Retrieve the executor for this actor as an optimized, unowned /// reference. /// /// This property must always evaluate to the same executor for a /// given actor instance, and holding on to the actor must keep the /// executor alive. /// /// This property will be implicitly accessed when work needs to be /// scheduled onto this actor. These accesses may be merged, /// eliminated, and rearranged with other work, and they may even /// be introduced when not strictly required. Visible side effects /// are therefore strongly discouraged within this property. nonisolated var unownedExecutor: UnownedSerialExecutor { get } }
Swift
복사
Actor는 class의 일종이며, 동시에 Sendable 프로토콜을 만족합니다.
actor에 전달된 값이 executor를 통해서 실행됩니다.(unownedExecutor)

Sendable

동시성 문제에 대해서 컴파일러 오류를 일으키는 역할을 담당하는 프로토콜
Sendable 프로토콜을 만족시킨다 == 서로 다른 스레드 간에서 공유되어도 문제 발생하지 않는다
대표적인 Sendable 프로토콜을 만족시킨 예
숫자(값), 불변 객체, 내부적인 동기화 알고리즘을 가진 객체
Actor Protocol, sendable 만족
동기화 처리가 되어있어서 Sendable를 만족하며, actor 자체를 다른 actor간에도 공유 가능
안정적인 스레드 간 전달이 가능하고 이를 위반하는 로직은 컴파일 오류가 발생

Executor

SerialExecutor, 들어온 요청을 순서대로 실행하는 기능을 수행
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) @frozen public struct UnownedSerialExecutor { @inlinable public init<E>(ordinary executor: E) where E : SerialExecutor }
Swift
복사
actor가 가지고 있는 UnownedSerialExecutor는 성능을 위해서 unowned하고 있을 뿐
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) public protocol Executor : AnyObject, Sendable { func enqueue(_ job: UnownedJob) } @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) public protocol SerialExecutor : Executor { func enqueue(_ job: UnownedJob) func asUnownedSerialExecutor() -> UnownedSerialExecutor } @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) @frozen public struct UnownedJob { public func run() }
Swift
복사
actor의 함수를 호출하면 이 함수의 실행이 UnownedJob으로 변경
SerialExecutor로 전달된 다음 실행
Example
actor에서 동시성 문제를 처리해주는 부분은 executor이며, executor가 들어오는 작업들을 순서대로 처리해준다.

actor

기본 actor - 기본 Executor가 스레드 풀을 사용하여 작업을 수행
Main actor - Executor가 DispatchQueue.main.async를 사용해서 작업을 수행