IOS swift 병렬 프로그래밍(Concurrency Programming)

in #ios6 years ago (edited)

개발도구 : xcode 9.4

언어 및 버전 : swift 4.1

swift 병렬 프로그래밍(Concurrency Programming)

  • Thread
  • operationQueue
  • GCD(Grand Centeral Dispatch)

1. Thread

쓰레드 선언은 다음과 같다.

let thread_one = Thread(target: self, selector: #selector(ViewController.runLoop_one), object: nil)
thread_one.start()
let thread_two = Thread(target: self, selector: #selector(ViewController.runLoop_two), object: nil)
thread_two.start()
let thread_three = Thread(target: self, selector: #selector(ViewController.runLoop_three), object: nil)
thread_three.start()

등록한 쓰레드 셀렉터(단순 숫자 카운트)

@objc func runLoop_one() {
    let nCountLimit = 100
    var nLoop = 0
    while(nLoop < nCountLimit){
          print("[thread one] : " + String(nLoop))
         nLoop = nLoop + 1
         Thread.sleep(forTimeInterval: 1)
    }
}
@objc func runLoop_two() {
    let nCountLimit = 100
    var nLoop = 0
    while(nLoop < nCountLimit){
         print("[thread two] : " + String(nLoop))
         nLoop = nLoop + 1
         Thread.sleep(forTimeInterval: 1)
    }
}
@objc func runLoop_three() {
    let nCountLimit = 100
    var nLoop = 0
    while(nLoop < nCountLimit){
         print("[thread three] : " + String(nLoop)) 
         nLoop = nLoop + 1
         Thread.sleep(forTimeInterval: 1)
    }
}

2. operationQueue

let operationQueue: OperationQueue = OperationQueue()
operationQueue.addOperation {
    let nCountLimit = 100
    var nLoop = 0
    while(nLoop < nCountLimit){
         print("[operationQueue one] : " + String(nLoop))
         nLoop = nLoop + 1
         Thread.sleep(forTimeInterval: 0.2)
    }
}
operationQueue.addOperation {
    let nCountLimit = 100
    var nLoop = 0
    while(nLoop < nCountLimit){
         print("[operationQueue two] : " + String(nLoop))
         nLoop = nLoop + 1
         Thread.sleep(forTimeInterval: 0.2)
    }
}
operationQueue.addOperation {
    let nCountLimit = 100
    var nLoop = 0
    while(nLoop < nCountLimit){
         print("[operationQueue three] : " + String(nLoop))
         nLoop = nLoop + 1
         Thread.sleep(forTimeInterval: 0.2)
    }
}

3. GCD Dispatch

  • main
  • global
  • custom

3.1. main

라벨과 같은 UI 객체를 컨드롤 할 수 있습니다. 

DispatchQueue.main.async {
    for i in 1...100 {
         self.m_strNum_1.text = String(i)
    }
}

3.2. global

DispatchQueue.global().async {
    for i in 1...100 {
        print("[DispatchQueue global] = \(i)")
    }
}

3.3. custom

DispatchQueue(label: "my define queue").async {
    for i in 1...100 {
         print("[my define queue] = \(i)")
    }
}

4. sync와 async

let globalQueue = DispatchQueue.global()        
globalQueue.async {
    for i in 1...100 {
                print("[DispatchQueue global async one] = \(i)")
    }        
}         
globalQueue.async {
    for i in 1...100 {
                print("[DispatchQueue global async two] = \(i)")
    }        
}         
globalQueue.async {
    for i in 1...100 {
         print("[DispatchQueue global async three] = \(i)")
    }        
}         
globalQueue.sync {
     for i in 1...100 {
         print("[DispatchQueue global sync one] = \(i)")
     }        
}         
globalQueue.sync {
    for i in 1...100 {
         print("[DispatchQueue global sync two] = \(i)")
    }        
}         
globalQueue.sync {
            for i in 1...100 {
                print("[DispatchQueue global sync three] = \(i)")
            }        
}

5. 여러 쓰레드를 묶어서 처리할 수 있습니다.

  • wait
  • notify

5.1. wait 사용

let group = DispatchGroup()        
group.enter()        
DispatchQueue.global().async {
    for i in 1...100 {
         print("[DispatchQueue global async one] = \(i)")
    }
    group.leave()        
}         
group.enter()        
DispatchQueue.global().async {
    for i in 1...100 {
         print("[DispatchQueue global async two] = \(i)")
    }
    group.leave()        
}         
group.wait(timeout: .distantFuture)        
print("two thread end")

5.2. notify 사용

let group = DispatchGroup()        
let queue = DispatchQueue.global()        
queue.async(group: group) {
    for i in 1...100 {
         print("[DispatchQueue global async one] = \(i)")
    }        
}        
queue.async(group: group) {
    for i in 1...100 {
         print("[DispatchQueue global async two] = \(i)")
    }        
}         
group.notify(queue: DispatchQueue.global()) {
    print("two thread end")        
}

Sort:  

Go here https://steemit.com/@a-a-a to get your post resteemed to over 72,000 followers.

Coin Marketplace

STEEM 0.19
TRX 0.18
JST 0.032
BTC 88139.36
ETH 3066.37
USDT 1.00
SBD 2.76