Skip to content

Instantly share code, notes, and snippets.

@andreconghau
Last active October 9, 2020 07:02
Show Gist options
  • Save andreconghau/5b782f9dda965a01eb1547614d71afe6 to your computer and use it in GitHub Desktop.
Save andreconghau/5b782f9dda965a01eb1547614d71afe6 to your computer and use it in GitHub Desktop.
GCD in Swift 5
let queue = DispatchQueue(label: "queue_action_1")
queue.async {
for i in 1...5 {
print("async=" + String(i))
}
}
for i in 1...5 {
print("sync=" + String(i))
}
// DEMO về bất đồng bộ với GROUP limit timer khi dùng GCD
let queue2 = DispatchQueue(label: "queue_action_2")
let queue_group = DispatchGroup()
queue_group.enter()
queue2.async {
for i in 0...5 {
print("in group =" + String(i))
sleep(1)
}
queue_group.leave()
}
// Kiểm tra status sau 2s xe đã xử lý xong group phía trên hay chưa
let queue_status = queue_group.wait(timeout: DispatchTime.now() + 1)
print(queue_status)
for i in 1...5 {
print("out group =" + String(i))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment