Skip to content

Instantly share code, notes, and snippets.

@shoheiyokoyama
Last active November 18, 2016 13:58
Show Gist options
  • Save shoheiyokoyama/5299ab5a60f074361ce91dbe843ac775 to your computer and use it in GitHub Desktop.
Save shoheiyokoyama/5299ab5a60f074361ce91dbe843ac775 to your computer and use it in GitHub Desktop.
SwiftでConcurrency Programming ref: http://qiita.com/shoheiyokoyama/items/433ff8e6612d8d368875
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")
}
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")
// 同期でタスクを追加
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
(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
.....
excute queue3
excute queue2
excute queue1
All task done
Wait task
Excute sleep
End sleep
Task finished
// @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")
}
excute
excute 3 second after
public func async(group: DispatchGroup? = default, qos: DispatchQoS = default, flags: DispatchWorkItemFlags = default, execute work: @escaping @convention(block) () -> Swift.Void)
// 並列で実行
DispatchQueue.concurrentPerform(iterations: 5) { index in
print("index: \(index)")
}
index: 1
index: 3
index: 2
index: 0
index: 5
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)")
}
}
}
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
public class var main: DispatchQueue { get }
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()
change number to 10
number: 10
number: 10
number: 10
...
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()
}
}
let myGlobal = { … global contains initialization in a call to a closure … }()
_ = myGlobal // using myGlobal will invoke the initialization code only
lazy var once: Void = { print("excute only once") }()
once
once
once
once
//結果
//excute only once
DispatchQueue.main.async {
// excute in main thread
}
public class func global(qos: DispatchQoS.QoSClass = default) -> DispatchQueue
DispatchQueue.global().async {
// excute
}
public enum QoSClass {
case background
case utility
case `default`
case userInitiated
case userInteractive
case unspecified
}
// 直列
DispatchQueue(label: "com.GCD.privateSerialQueue").async {
// excute
}
// 並列
DispatchQueue(label: "com.GCD.privateConcurrentQueue", attributes: .concurrent).async {
// excute
}
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