Notice
Recent Posts
Recent Comments
Link
관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - swift] Sync vs Async vs Serial vs Concurrent(concurrency) (GCD) 본문

iOS 응용 (swift)

[iOS - swift] Sync vs Async vs Serial vs Concurrent(concurrency) (GCD)

jake-kim 2021. 7. 2. 01:25

Sync vs Async vs Serial vs Concurrent

  • Serial vs Concurrent: Queue에 들어온 작업 중, 앞 작업과 뒷 작업을 '순차적'으로 시킬것인지 아닐것인지 기준
  • sync vs serial
    • sync: 앞 작업과 뒷 작업의 연관성이 없고 오로지, 자신의 요청에 대한 답을 받을때까지 기다리는 것
    • serial: Queue에 들어온 작업들을 순차적으로 실행 (FIFO) - 예시) Main queue

  • 이론상, async한 작업을 Serial Queue에 넣으면 성능 저하가 될 수 있는 상태 (틀리다면 댓글 부탁드려요)
    • 성능 저하 상황: A작업이 async작업이며 Serial Queue에 있다면, A작업이 끝나지 전에 다음 작업 처리 준비 > 다음 작업을 처리하려고 했지만 Serial Queue라서 A작업 다시 재개 > A작업은 async이므로 다시 다음 작업 처리 준비 > ... 반복되고 A작업이 끝나면 다음 작업 시작
  • ex) async와 sync 예제
print("start")
DispatchQueue.main.async {
    for _ in 0...5 {
        print("async")
    }
}

for _ in 0...5 {
    print("sync")
}
print("end")

start
sync
sync
sync
sync
sync
sync
end
async
async
async
async
async
async

  • async vs concurrent
    • async: 단일 작업에 대한 개념 (테스크를 끝날때 까지 기다리지 않는 것)
    • concurrent: Queue에 들어온 작업들을 한번에 여러개 실행

let concurrentQueue = DispatchQueue.init(label: "SodeulQueue", attributes: .concurrent)
concurrentQueue.sync  { print("start") }
concurrentQueue.async { for _ in 0...5 { print("async") }}
concurrentQueue.sync  { for _ in 0...5 { print("sync") } }
concurrentQueue.sync  { print("end") }

start
sync
sync
sync
sync
sync
sync
async
async
end
async
async
async
async

코드로 이해

  • 핵심: Queue가 Serial/Concurrent 판단 후 안에 들어가는 작업들을 Sync/Async 판단
    • Serial/Concurrent 설정: queue객체 만들때 초기화 값으로 한번만 설정 가능
    • sync/async 설정: queue객체에서 함수로 부르는 것
  • sync와 concurrent
let myQueue = DispatchQueue(label: "myQueue", attributes: .concurrent)

for i in 1...5 {
    myQueue.sync {
        print("\(i)")
    }
}

1
2
3
4
5
  • async와 concurrent
let myQueue = DispatchQueue(label: "myQueue", attributes: .concurrent)

for i in 1...5 {
    myQueue.async {
        print("\(i)")
    }
}

1
3
4
5
2
  • concurrent queue와 sync, async
let myQueue = DispatchQueue(label: "myQueue", attributes: .concurrent)

for i in 1...5 {
    myQueue.async {
        print("async")
    }
    myQueue.sync {
        print("sync")
    }
}

sync
async
async
sync
sync
sync
sync
async
async
async
  • Seriual Queue와 sync, async
let myQueue = DispatchQueue(label: "myQueue")

for i in 1...5 {
    myQueue.async {
        print("async")
    }
    myQueue.sync {
        print("sync")
    }
}

async
sync
async
sync
async
sync
async
sync
async
sync

개념 체크 리스트

  • sync, async: 한 작업에 대한 특성
  • serial, concurrent: 작업들 다수를 어떻게 처리할까에 대한 것
  • sync는 '순차적'이라는 개념이 아닌, '단일 작업'에 대한 특성: 테스크에 대한 요청을 받을 때까지 기다리는 것
  • serial: 단일 작업들을 '하나씩' 실행하는 것

Main Queue

  • 앱 시작 > 'main' Dispatch Queue가 자동 생성: UI를 담당하는 serial queue
    • 자주 사용되기 때문에 Apple에서 클래스 변수로 사용 제공: 'DisaptchQueue.main'

* 심화

- 비동기 작업에서의 안전한 get, set 설정 방법 (GCD barrier)

- Deadlock(교착상태): DispatchQueue.main.sync 사용 주의 사항

 

* 참고

https://www.baeldung.com/cs/async-vs-multi-threading

https://babbab2.tistory.com/64

Comments