Last active
November 18, 2016 13:58
-
-
Save shoheiyokoyama/5299ab5a60f074361ce91dbe843ac775 to your computer and use it in GitHub Desktop.
SwiftでConcurrency Programming ref: http://qiita.com/shoheiyokoyama/items/433ff8e6612d8d368875
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let group = DispatchGroup() | |
let queue1 = DispatchQueue(label: "com.GCD.groupQueue1") | |
let queue2 = DispatchQueue(label: "com.GCD.groupQueue2") | |
let queue3 = DispatchQueue(label: "com.GCD.groupQueue3") | |
queue1.async(group: group) { | |
sleep(4) | |
print("excute queue1") | |
} | |
queue2.async(group: group) { | |
sleep(2) | |
print("excute queue2") | |
} | |
queue3.async(group: group) { | |
sleep(1) | |
print("excute queue3") | |
} | |
group.notify(queue: DispatchQueue.main) { | |
print("All task done") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let semaphone = DispatchSemaphore(value: 0) | |
let queue = DispatchQueue.global() | |
queue.async { | |
print("Excute sleep") | |
sleep(2) | |
print("End sleep") | |
semaphone.signal() | |
} | |
print("Wait task") | |
semaphone.wait() | |
print("Task finished") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 同期でタスクを追加 | |
DispatchQueue.global().sync { | |
sleep(1) | |
print("Excute Task") | |
} | |
print("Qiita") | |
// Excute Task | |
// Qiita | |
//非同期でタスクを追加 | |
DispatchQueue.global().async { | |
sleep(1) | |
print("Excute Task") | |
} | |
print("Qiita") | |
// Qiita | |
// Excute Task |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(0...10).forEach { index in | |
// 直列で実行 | |
serialQueue.async { | |
print("Index: \(index)") | |
} | |
// Index: 0 | |
// Index: 1 | |
// Index: 2 | |
..... | |
// 並列で実行 | |
concurrentQueue.async { | |
print("Index: \(index)") | |
} | |
// Index: 1 | |
// Index: 0 | |
// Index: 2 | |
..... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
excute queue3 | |
excute queue2 | |
excute queue1 | |
All task done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Wait task | |
Excute sleep | |
End sleep | |
Task finished |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @convention(c)はつけなくても良い | |
typealias CallBack = @convention(c) () -> Void | |
let closure: (DispatchQueue, @escaping CallBack) -> Void = { queue, callBack in | |
//3秒後にサブスレッドで実行 | |
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(3)) { | |
queue.async(execute: callBack) | |
} | |
} | |
let queue = DispatchQueue.global() | |
closure(queue) { | |
print("excute 3 second after") | |
} | |
queue.async { | |
print("excute") | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
excute | |
excute 3 second after |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func async(group: DispatchGroup? = default, qos: DispatchQoS = default, flags: DispatchWorkItemFlags = default, execute work: @escaping @convention(block) () -> Swift.Void) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 並列で実行 | |
DispatchQueue.concurrentPerform(iterations: 5) { index in | |
print("index: \(index)") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
index: 1 | |
index: 3 | |
index: 2 | |
index: 0 | |
index: 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let queue = DispatchQueue(label: "com.GCD.barrier", attributes: .concurrent) | |
(0...100).forEach { index in | |
// 直列で実行 | |
if 30...50 ~= index { | |
queue.async(flags: .barrier) { | |
print("excute index: \(index)") | |
} | |
} else { | |
// 並列で実行 | |
queue.async { | |
print("excute index: \(index)") | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
excute index: 4 | |
excute index: 0 | |
excute index: 6 | |
... | |
excute index: 30 | |
excute index: 31 | |
excute index: 32 | |
excute index: 33 | |
excute index: 34 | |
... | |
excute index: 75 | |
excute index: 81 | |
excute index: 77 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class var main: DispatchQueue { get } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let queue = DispatchQueue(label: "com.GCD.privateQueue") | |
// 一時停止 | |
queue.suspend() | |
var number = 30 | |
(0...10).forEach { index in queue.async { print("number: \(number)") } } | |
// デバックをわかりやすくするためにsleep | |
sleep(1) | |
number = 10 | |
print("change number to 10") | |
// 再開 | |
queue.resume() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
change number to 10 | |
number: 10 | |
number: 10 | |
number: 10 | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let semaphone = DispatchSemaphore(value: 2) | |
let queue = DispatchQueue.global() | |
(0...10).forEach { index in | |
queue.async { | |
semaphone.wait() | |
print("Excute sleep: \(index)") | |
sleep(2) | |
print("End sleep: \(index)") | |
semaphone.signal() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let myGlobal = { … global contains initialization in a call to a closure … }() | |
_ = myGlobal // using myGlobal will invoke the initialization code only |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lazy var once: Void = { print("excute only once") }() | |
once | |
once | |
once | |
once | |
//結果 | |
//excute only once |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DispatchQueue.main.async { | |
// excute in main thread | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class func global(qos: DispatchQoS.QoSClass = default) -> DispatchQueue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DispatchQueue.global().async { | |
// excute | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum QoSClass { | |
case background | |
case utility | |
case `default` | |
case userInitiated | |
case userInteractive | |
case unspecified | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 直列 | |
DispatchQueue(label: "com.GCD.privateSerialQueue").async { | |
// excute | |
} | |
// 並列 | |
DispatchQueue(label: "com.GCD.privateConcurrentQueue", attributes: .concurrent).async { | |
// excute | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(5)) { | |
// excute 5 second after | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment