Swift GCD 死锁
xxxixxxx

1. 串行队列中,在异步任务中添加同步任务

1
2
3
4
5
6
7
8
9
10
11
12
13
/// 由于没有定义 attributes 所以是串行队列
let queue = DispatchQueue(label: "myQueue")
queue.async {
print("----- task 1 -------")
queue.sync {
print("----- task 2 -------")
}
}

⚠️解决方法
/// 解决方法将串行改为并行
let queue = DispatchQueue(label: "myQueue", attributes: DispatchQueue.Attributes.concurrent)

2. 主线程同步

1
2
3
4
let queue = DispatchQueue.main
queue.sync {
print("----- task 1 -------")
}
  • Post title:Swift GCD 死锁
  • Post author:xxxixxxx
  • Create time:2020-11-04 00:00:00
  • Post link:https://xxxixxx.github.io/2020/11/04/100-Swift-GCD-死锁/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.
 Comments