iOS-OC底层21:GCD 函数 和队列
GCD简介
1.什么是GCD?
全称是 Grand Central Dispatch ,将任务添加到队列,并且指定执行任务的函数
纯 C 语言,提供了非常多强大的函数.
2.GCD的优势
GCD 是苹果公司为多核的并行运算提出的解决方案
GCD 会自动利用更多的CPU内核(比如双核、四核)
GCD 会自动管理线程的生命周期(创建线程、调度任务、销毁线程)
程序员只需要告诉 GCD 想要执行什么任务,不需要编写任何线程管理代码
函数
任务使用 block 封装
任务的 block 没有参数也没有返回值
执行任务的函数
异步 dispatch_async
不用等待当前语句执行完毕,就可以执行下一条语句,会开启线程执行 block 的任务,异步是多线程的代名词
同步 dispatch_sync
必须等待当前语句执行完毕,才会执行下一条语句,不会开启线程,在当前执行block的任务
队列
image.png函数与队列演示
同步函数串行队列
不会开启线程,在当前线程执行任务,任务串行执行,任务一个接着一个,会产生堵塞。
-(void)syncSerial {
dispatch_queue_t queue = dispatch_queue_create("com.my.test", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
sleep(2);
NSLog(@"任务1%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
sleep(2);
NSLog(@"任务2%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
sleep(2);
NSLog(@"任务3%@",[NSThread currentThread]);
});
}
2020-11-04 14:20:48.794288+0800 001---函数与队列[30624:126102] 任务1<NSThread: 0x600003b64500>{number = 1, name = main}
2020-11-04 14:20:50.795558+0800 001---函数与队列[30624:126102] 任务2<NSThread: 0x600003b64500>{number = 1, name = main}
2020-11-04 14:20:52.796567+0800 001---函数与队列[30624:126102] 任务3<NSThread: 0x600003b64500>{number = 1, name = main}
同步函数并发执行
不会开启线程,在当前线程执行,任务一个接着一个
-(void)syncConcurrent {
dispatch_queue_t queue = dispatch_queue_create("com.my.test", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
sleep(2);
NSLog(@"任务1%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
sleep(2);
NSLog(@"任务2%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
sleep(2);
NSLog(@"任务3%@",[NSThread currentThread]);
});
}
2020-11-04 14:23:18.411377+0800 001---函数与队列[32332:133705] 任务1<NSThread: 0x60000071cac0>{number = 1, name = main}
2020-11-04 14:23:20.411815+0800 001---函数与队列[32332:133705] 任务2<NSThread: 0x60000071cac0>{number = 1, name = main}
2020-11-04 14:23:22.412347+0800 001---函数与队列[32332:133705] 任务3<NSThread: 0x60000071cac0>{number = 1, name = main}
异步函数串行队列
有可能开启一条新线程,任务一个接着一个
-(void)asyncSerial {
dispatch_queue_t queue = dispatch_queue_create("com.my.test", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
sleep(2);
NSLog(@"任务1%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
sleep(2);
NSLog(@"任务2%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
sleep(2);
NSLog(@"任务3%@",[NSThread currentThread]);
});
}
2020-11-04 14:30:11.772931+0800 001---函数与队列[36921:152296] 任务1<NSThread: 0x600003203a00>{number = 6, name = (null)}
2020-11-04 14:30:13.777365+0800 001---函数与队列[36921:152296] 任务2<NSThread: 0x600003203a00>{number = 6, name = (null)}
2020-11-04 14:30:15.777809+0800 001---函数与队列[36921:152296] 任务3<NSThread: 0x600003203a00>{number = 6, name = (null)}
异步函数并行队列
开启线程,任务异步执行,没有顺序CPU调度有关
-(void)asyncConcurrent {
dispatch_queue_t queue = dispatch_queue_create("com.my.test", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
sleep(2);
NSLog(@"任务1%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
sleep(2);
NSLog(@"任务2%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
sleep(2);
NSLog(@"任务3%@",[NSThread currentThread]);
});
}
2020-11-04 14:26:44.864767+0800 001---函数与队列[34602:142790] 任务3<NSThread: 0x600003c6b880>{number = 8, name = (null)}
2020-11-04 14:26:44.864767+0800 001---函数与队列[34602:142791] 任务1<NSThread: 0x600003c2db40>{number = 3, name = (null)}
2020-11-04 14:26:44.864781+0800 001---函数与队列[34602:142792] 任务2<NSThread: 0x600003c21a40>{number = 4, name = (null)}
主队列
专⻔用来在主线程上调度任务的串行队列,不会开启线程,如果当前主线程正在有任务执行,那么无论主队列中当前被添加了什么任务,都不会被调度 dispatch_get_main_queue();
全局队列
为了方便程序员的使用,苹果提供了全局队列 dispatch_get_global_queue(0, 0),全局队列是一个并发队列,在使用多线程开发时,如果对队列没有特殊需求,在执行异步任务时,可以直接使用全局队列。dispatch_get_global_queue()
死锁现象
主线程因为你同步函数的原因等着先执行任务,主队列等着主线程的任务执行完毕再执行自己的任务,主队列和主线程相互等待会造成死锁
image.png
GCD源码分析
队列
我们先看一下队列是什么类型
dispatch_queue_t serial = dispatch_queue_create("cooci", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t conque = dispatch_queue_create("cooci", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_queue_t globQueue = dispatch_get_global_queue(0, 0);
NSLog(@"\n%@\n%@\n%@\n%@\n",serial,conque,mainQueue,globQueue);
打印日志
<OS_dispatch_queue_serial: cooci>
<OS_dispatch_queue_concurrent: cooci>
<OS_dispatch_queue_main: com.apple.main-thread>
<OS_dispatch_queue_global: com.apple.root.default-qos>
(lldb) p object_getClass(mainQueue)
(Class _Nullable) $0 = OS_dispatch_queue_main
(lldb) p object_getClass(globQueue)
(Class _Nullable) $1 = OS_dispatch_queue_global
(lldb) p object_getClass(serial)
(Class _Nullable) $2 = OS_dispatch_queue_serial
(lldb) p object_getClass(conque)
(Class _Nullable) $3 = OS_dispatch_queue_concurrent
我们看出队列是对象
主队列
dispatch_queue_main_t
dispatch_get_main_queue(void)
{
return DISPATCH_GLOBAL_OBJECT(dispatch_queue_main_t, _dispatch_main_q);
}
_dispatch_main_q在libDispatch中定义
image.png
DISPATCH_GLOBAL_OBJECT的含义是类型转化,dispatch_queue_main_t通过追踪是拼接的一个类名OS_dispatch_queue_main
这个主队列是什么时候创建的呢?
在初始化的libdispatch_init中我们看到如下代码
_dispatch_queue_set_current(&_dispatch_main_q);
_dispatch_queue_set_bound_thread(&_dispatch_main_q);
全局队列
_dispatch_root_queues中保存了全局队列,所属的类为OS_dispatch_queue_global,DISPATCH_GLOBAL_OBJECT_HEADER表示了对象所属的类,dq_serialnum显示了并发。
image.png
创建的队列
创建对象dispatch_queue_create=======>_dispatch_lane_create_with_target
dispatch_queue_create创建的队列可能是并发或者串行的,通过下面代码区分
if (dqai.dqai_concurrent) {
// OS_dispatch_queue_concurrent
vtable = DISPATCH_VTABLE(queue_concurrent);
} else {
vtable = DISPATCH_VTABLE(queue_serial);
}
返回对象的初始化如下
dispatch_lane_t dq = _dispatch_object_alloc(vtable,
sizeof(struct dispatch_lane_s));
_dispatch_queue_init(dq, dqf, dqai.dqai_concurrent ?
DISPATCH_QUEUE_WIDTH_MAX : 1, DISPATCH_QUEUE_ROLE_INNER |
(dqai.dqai_inactive ? DISPATCH_QUEUE_INACTIVE : 0));
_dispatch_object_alloc
_dispatch_object_alloc===> _os_object_alloc_realized
inline _os_object_t
_os_object_alloc_realized(const void *cls, size_t size)
{
_os_object_t obj;
dispatch_assert(size >= sizeof(struct _os_object_s));
while (unlikely(!(obj = calloc(1u, size)))) {
_dispatch_temporary_resource_shortage();
}
obj->os_obj_isa = cls;
return obj;
}
_dispatch_queue_init
_dispatch_queue_init中确定并发和串行,如果是并发则设置最大并发数DISPATCH_QUEUE_WIDTH_MAX = 4092,串行则为1