GCD 使用过程中的出现的死锁问题
2016-06-23 本文已影响36人
Laughingg
死锁 1、 主队列在主线程同步执行
下列代码写在 viewDidLoad
中:
NSLog(@"before: %@", [NSThread currentThread]);
// 创建一个队列(串行)
dispatch_queue_t q = dispatch_get_main_queue();
// 5 次异步
for (int index = 0; index < 5; index ++) {
// 将任务添加到队列
dispatch_sync(q, ^{
NSLog(@"task : %@", [NSThread currentThread]);
});
}
NSLog(@"after: %@", [NSThread currentThread]);
打印结果:
2016-06-21 12:07:22.921 Thread-Objc[44793:2467944] before: <NSThread: 0x7fb84a500ac0>{number = 1, name = main}
测试的结果是 : 死锁
关于死锁:记住主队列的特点就容易理解!主线程有任务就暂时不调度任务!
上面死锁的写法是: ** 主队列在主线程同步执行任务** 死锁。
死锁 2、
NSLog(@"before: %@", [NSThread currentThread]);
dispatch_queue_t q = dispatch_queue_create("q1", DISPATCH_QUEUE_SERIAL);
for (int index = 0; index < 10; index ++) {
dispatch_async(q, ^{
NSLog(@"async : %@", [NSThread currentThread]);
dispatch_sync(q, ^{
NSLog(@"sync :%@", [NSThread currentThread]);
});
});
}
NSLog(@"after: %@", [NSThread currentThread]);
打印结果:
2016-06-23 22:24:46.583 Thread-Objc[14719:2440315] before: <NSThread: 0x7fd989701580>{number = 1, name = main}
2016-06-23 22:24:46.583 Thread-Objc[14719:2440315] after: <NSThread: 0x7fd989701580>{number = 1, name = main}
2016-06-23 22:24:46.584 Thread-Objc[14719:2440510] async :<NSThread: 0x7fd98971c4b0>{number = 2, name = (null)}
上面死锁的写法是: 串行队列异步执行,根据特点会开 1 条线程,然后再在当前队列线程进行同步执行 死锁。
死锁的共同特点是: 在当前队列的当前线程执行同步任务, 死锁!
在 dispatch_sync 函数的 AIP 中有这么一段解释:
- Calls to dispatch_sync() targeting the current queue will result
- in dead-lock. Use of dispatch_sync() is also subject to the same
- multi-party dead-lock problems that may result from the use of a mutex.
- Use of dispatch_async() is preferred.
- 在当前队列调用 dispatch_sync() 会造成死锁。
- 在互斥锁里面使用 dispatch_sync() 也会造成同样的问题。
- 优先考虑使用 dispatch_async()