1.GCD概念:将任务添加到队列,并且指定执行任务的函数
GCD 简介
什么是GCD?
全称是 Grand Central Dispatch
纯 C 语言,提供了非常多强大的函数
GCD的优势
GCD 是苹果公司为多核的并行运算提出的解决方案
GCD 会自动利用更多的CPU内核(比如双核、四核)
GCD 会自动管理线程的生命周期(创建线程、调度任务、销毁线程) 程序员只需要告诉 GCD 想要执行什么任务,不需要编写任何线程管理代码
2.函数和队列
任务使用 block 封装
任务的 block 没有参数也没有返回值
执行任务的函数
- 1.异步
dispatch_async
不用等待当前语句执行完毕,就可以执行下一条语句
会开启线程执行 block 的任务
异步是多线程的代名词 - 2.同步
dispatch_sync
必须等待当前语句执行完毕,才会执行下一条语句
不会开启线程
在当前执行 block 的任务
image.png
image.png
3.同步和异步都会耗时:用于解决并发和多线程问题
-
1.只在主队列执行任务执行时间:0.000003
image.png -
2.异步任务耗时:0.000019
image.png -
3.同步任务耗时:0.000008
image.png
说明异步比较耗时
4.面试题
image.png若主线程睡眠,则结果不同
image.png image.png
NULL代表串行队列#define DISPATCH_QUEUE_SERIAL NULL
image.png
image.png
5.四种队列类型
串行队列(<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>
)
主队列
专⻔用来在主线程上调度任务的串行队列
不会开启线程 如果当前主线程正在有任务执行,那么无论主队列中当前被添加了什么任务,都不会被调度 dispatch_get_main_queue();
image.png
通过下符号断点的方式知道GCD源码所在的库为libdispatch.dylib
在libdispatch的源码中搜索主队类label的名字,入口在libdispatch_init()方法,在dyld链接libdispatch初始化的时候
image.png
image.png
_objc_init堆栈@2x.png
在iOS-底层原理12-应用程序加载中知道libdispatch初始化,由堆栈得知,libdispatch_init代码来自于libdispatch.dylib的libdispatch_init
void
libdispatch_init(void)
{
dispatch_assert(sizeof(struct dispatch_apply_s) <=
DISPATCH_CONTINUATION_SIZE);
if (_dispatch_getenv_bool("LIBDISPATCH_STRICT", false)) {
_dispatch_mode |= DISPATCH_MODE_STRICT;
}
#if HAVE_OS_FAULT_WITH_PAYLOAD && TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
if (_dispatch_getenv_bool("LIBDISPATCH_NO_FAULTS", false)) {
_dispatch_mode |= DISPATCH_MODE_NO_FAULTS;
} else if (getpid() == 1 ||
!os_variant_has_internal_diagnostics("com.apple.libdispatch")) {
_dispatch_mode |= DISPATCH_MODE_NO_FAULTS;
}
#endif // HAVE_OS_FAULT_WITH_PAYLOAD && TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
#if DISPATCH_DEBUG || DISPATCH_PROFILE
#if DISPATCH_USE_KEVENT_WORKQUEUE
if (getenv("LIBDISPATCH_DISABLE_KEVENT_WQ")) {
_dispatch_kevent_workqueue_enabled = false;
}
#endif
#endif
#if HAVE_PTHREAD_WORKQUEUE_QOS
dispatch_qos_t qos = _dispatch_qos_from_qos_class(qos_class_main());
_dispatch_main_q.dq_priority = _dispatch_priority_make(qos, 0);
#if DISPATCH_DEBUG
if (!getenv("LIBDISPATCH_DISABLE_SET_QOS")) {
_dispatch_set_qos_class_enabled = 1;
}
#endif
#endif
#if DISPATCH_USE_THREAD_LOCAL_STORAGE
_dispatch_thread_key_create(&__dispatch_tsd_key, _libdispatch_tsd_cleanup);
#else
_dispatch_thread_key_create(&dispatch_priority_key, NULL);
_dispatch_thread_key_create(&dispatch_r2k_key, NULL);
_dispatch_thread_key_create(&dispatch_queue_key, _dispatch_queue_cleanup);
_dispatch_thread_key_create(&dispatch_frame_key, _dispatch_frame_cleanup);
_dispatch_thread_key_create(&dispatch_cache_key, _dispatch_cache_cleanup);
_dispatch_thread_key_create(&dispatch_context_key, _dispatch_context_cleanup);
_dispatch_thread_key_create(&dispatch_pthread_root_queue_observer_hooks_key,
NULL);
_dispatch_thread_key_create(&dispatch_basepri_key, NULL);
#if DISPATCH_INTROSPECTION
_dispatch_thread_key_create(&dispatch_introspection_key , NULL);
#elif DISPATCH_PERF_MON
_dispatch_thread_key_create(&dispatch_bcounter_key, NULL);
#endif
_dispatch_thread_key_create(&dispatch_wlh_key, _dispatch_wlh_cleanup);
_dispatch_thread_key_create(&dispatch_voucher_key, _voucher_thread_cleanup);
_dispatch_thread_key_create(&dispatch_deferred_items_key,
_dispatch_deferred_items_cleanup);
#endif
#if DISPATCH_USE_RESOLVERS // rdar://problem/8541707
_dispatch_main_q.do_targetq = _dispatch_get_default_queue(true);
#endif
_dispatch_queue_set_current(&_dispatch_main_q);
_dispatch_queue_set_bound_thread(&_dispatch_main_q);
#if DISPATCH_USE_PTHREAD_ATFORK
(void)dispatch_assume_zero(pthread_atfork(dispatch_atfork_prepare,
dispatch_atfork_parent, dispatch_atfork_child));
#endif
_dispatch_hw_config_init();
_dispatch_time_init();
_dispatch_vtable_init();
_os_object_init();
_voucher_init();
_dispatch_introspection_init();
}
取主队列地址绑定到当前的线程
全局并发队列
为了方便程序员的使用,苹果提供了全局队列 dispatch_get_global_queue(0, 0) 全局队列是一个并发队列 在使用多线程开发时,如果对队列没有特殊需求,在执行异步任务时,可以直接使用全局队 列
同理搜索com.apple.root.default,为静态结构体
struct dispatch_queue_global_s _dispatch_root_queues[]
,和并发队列一样,有一点区别是#define DISPATCH_QUEUE_WIDTH_POOL (DISPATCH_QUEUE_WIDTH_FULL - 1),而并发队列中#define DISPATCH_QUEUE_WIDTH_MAX (DISPATCH_QUEUE_WIDTH_FULL - 2)image.png
image.png
6.队列怎么创建,串行队列和并行队列创建过程
image.pngimage.png
dispatch_queue_create("cooci", DISPATCH_QUEUE_SERIAL)传入的第二个参数决定了MAX和1的区别,队列也是对象,也要通过alloc,init创建,也有isa指向类的类型class:OS_dispatch_queue_serial_class
image.png
队列是对象,通过底层C函数创建
-
面试题:全局并发队列的最大容积:4096(16的三次方)-1,一般的并发队列的最大容积:4096(16的三次方)-2
image.png
7.异步函数dispatch_async源码分析
image.png任务包装器->接收->保存work,后续执行block任务work,保存_dispatch_call_block_and_release,后续调用block完成后执行进行析构和释放
查看异步函数并发队列堆栈信息
* thread #4, queue = 'cooci', stop reason = breakpoint 1.1
* frame #0: 0x000000010b60b157 001---函数与队列`__29-[ViewController viewDidLoad]_block_invoke(.block_descriptor=0x000000010b60e0e8) at ViewController.m:42:9
frame #1: 0x000000010b876f11 libdispatch.dylib`_dispatch_call_block_and_release + 12
frame #2: 0x000000010b877e8e libdispatch.dylib`_dispatch_client_callout + 8
frame #3: 0x000000010b87a7a3 libdispatch.dylib`_dispatch_continuation_pop + 552
frame #4: 0x000000010b879bbb libdispatch.dylib`_dispatch_async_redirect_invoke + 771
frame #5: 0x000000010b889399 libdispatch.dylib`_dispatch_root_queue_drain + 351
frame #6: 0x000000010b889ca6 libdispatch.dylib`_dispatch_worker_thread2 + 135
frame #7: 0x00007fff523019f7 libsystem_pthread.dylib`_pthread_wqthread + 220
frame #8: 0x00007fff52300b77 libsystem_pthread.dylib`start_wqthread + 15
image.png
由堆栈信息得出libsystem_pthread.dylib中调用了libdispatch.dylib
image.png
网友评论