GCD簡介
1.什么是GCD?
全稱是 Grand Central Dispatch ,將任務(wù)添加到隊(duì)列,并且指定執(zhí)行任務(wù)的函數(shù)
純 C 語言,提供了非常多強(qiáng)大的函數(shù).
2.GCD的優(yōu)勢
GCD 是蘋果公司為多核的并行運(yùn)算提出的解決方案
GCD 會(huì)自動(dòng)利用更多的CPU內(nèi)核(比如雙核姻乓、四核)
GCD 會(huì)自動(dòng)管理線程的生命周期(創(chuàng)建線程、調(diào)度任務(wù)血筑、銷毀線程)
程序員只需要告訴 GCD 想要執(zhí)行什么任務(wù)嫌吠,不需要編寫任何線程管理代碼
函數(shù)
任務(wù)使用 block 封裝
任務(wù)的 block 沒有參數(shù)也沒有返回值
執(zhí)行任務(wù)的函數(shù)
異步 dispatch_async
不用等待當(dāng)前語句執(zhí)行完畢,就可以執(zhí)行下一條語句搁拙,會(huì)開啟線程執(zhí)行 block 的任務(wù)秒梳,異步是多線程的代名詞
同步 dispatch_sync
必須等待當(dāng)前語句執(zhí)行完畢,才會(huì)執(zhí)行下一條語句箕速,不會(huì)開啟線程酪碘,在當(dāng)前執(zhí)行block的任務(wù)
隊(duì)列
函數(shù)與隊(duì)列演示
同步函數(shù)串行隊(duì)列
不會(huì)開啟線程,在當(dāng)前線程執(zhí)行任務(wù)盐茎,任務(wù)串行執(zhí)行兴垦,任務(wù)一個(gè)接著一個(gè),會(huì)產(chǎn)生堵塞。
-(void)syncSerial {
dispatch_queue_t queue = dispatch_queue_create("com.my.test", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
sleep(2);
NSLog(@"任務(wù)1%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
sleep(2);
NSLog(@"任務(wù)2%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
sleep(2);
NSLog(@"任務(wù)3%@",[NSThread currentThread]);
});
}
2020-11-04 14:20:48.794288+0800 001---函數(shù)與隊(duì)列[30624:126102] 任務(wù)1<NSThread: 0x600003b64500>{number = 1, name = main}
2020-11-04 14:20:50.795558+0800 001---函數(shù)與隊(duì)列[30624:126102] 任務(wù)2<NSThread: 0x600003b64500>{number = 1, name = main}
2020-11-04 14:20:52.796567+0800 001---函數(shù)與隊(duì)列[30624:126102] 任務(wù)3<NSThread: 0x600003b64500>{number = 1, name = main}
同步函數(shù)并發(fā)執(zhí)行
不會(huì)開啟線程探越,在當(dāng)前線程執(zhí)行狡赐,任務(wù)一個(gè)接著一個(gè)
-(void)syncConcurrent {
dispatch_queue_t queue = dispatch_queue_create("com.my.test", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
sleep(2);
NSLog(@"任務(wù)1%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
sleep(2);
NSLog(@"任務(wù)2%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
sleep(2);
NSLog(@"任務(wù)3%@",[NSThread currentThread]);
});
}
2020-11-04 14:23:18.411377+0800 001---函數(shù)與隊(duì)列[32332:133705] 任務(wù)1<NSThread: 0x60000071cac0>{number = 1, name = main}
2020-11-04 14:23:20.411815+0800 001---函數(shù)與隊(duì)列[32332:133705] 任務(wù)2<NSThread: 0x60000071cac0>{number = 1, name = main}
2020-11-04 14:23:22.412347+0800 001---函數(shù)與隊(duì)列[32332:133705] 任務(wù)3<NSThread: 0x60000071cac0>{number = 1, name = main}
異步函數(shù)串行隊(duì)列
有可能開啟一條新線程,任務(wù)一個(gè)接著一個(gè)
-(void)asyncSerial {
dispatch_queue_t queue = dispatch_queue_create("com.my.test", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
sleep(2);
NSLog(@"任務(wù)1%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
sleep(2);
NSLog(@"任務(wù)2%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
sleep(2);
NSLog(@"任務(wù)3%@",[NSThread currentThread]);
});
}
2020-11-04 14:30:11.772931+0800 001---函數(shù)與隊(duì)列[36921:152296] 任務(wù)1<NSThread: 0x600003203a00>{number = 6, name = (null)}
2020-11-04 14:30:13.777365+0800 001---函數(shù)與隊(duì)列[36921:152296] 任務(wù)2<NSThread: 0x600003203a00>{number = 6, name = (null)}
2020-11-04 14:30:15.777809+0800 001---函數(shù)與隊(duì)列[36921:152296] 任務(wù)3<NSThread: 0x600003203a00>{number = 6, name = (null)}
異步函數(shù)并行隊(duì)列
開啟線程钦幔,任務(wù)異步執(zhí)行枕屉,沒有順序CPU調(diào)度有關(guān)
-(void)asyncConcurrent {
dispatch_queue_t queue = dispatch_queue_create("com.my.test", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
sleep(2);
NSLog(@"任務(wù)1%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
sleep(2);
NSLog(@"任務(wù)2%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
sleep(2);
NSLog(@"任務(wù)3%@",[NSThread currentThread]);
});
}
2020-11-04 14:26:44.864767+0800 001---函數(shù)與隊(duì)列[34602:142790] 任務(wù)3<NSThread: 0x600003c6b880>{number = 8, name = (null)}
2020-11-04 14:26:44.864767+0800 001---函數(shù)與隊(duì)列[34602:142791] 任務(wù)1<NSThread: 0x600003c2db40>{number = 3, name = (null)}
2020-11-04 14:26:44.864781+0800 001---函數(shù)與隊(duì)列[34602:142792] 任務(wù)2<NSThread: 0x600003c21a40>{number = 4, name = (null)}
主隊(duì)列
專?用來在主線程上調(diào)度任務(wù)的串行隊(duì)列,不會(huì)開啟線程,如果當(dāng)前主線程正在有任務(wù)執(zhí)行,那么無論主隊(duì)列中當(dāng)前被添加了什么任務(wù)鲤氢,都不會(huì)被調(diào)度 dispatch_get_main_queue();
全局隊(duì)列
為了方便程序員的使用搀擂,蘋果提供了全局隊(duì)列 dispatch_get_global_queue(0, 0),全局隊(duì)列是一個(gè)并發(fā)隊(duì)列卷玉,在使用多線程開發(fā)時(shí)哨颂,如果對隊(duì)列沒有特殊需求,在執(zhí)行異步任務(wù)時(shí)相种,可以直接使用全局隊(duì)列威恼。dispatch_get_global_queue()
死鎖現(xiàn)象
主線程因?yàn)槟阃胶瘮?shù)的原因等著先執(zhí)行任務(wù),主隊(duì)列等著主線程的任務(wù)執(zhí)行完畢再執(zhí)行自己的任務(wù),主隊(duì)列和主線程相互等待會(huì)造成死鎖
GCD源碼分析
隊(duì)列
我們先看一下隊(duì)列是什么類型
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
我們看出隊(duì)列是對象
主隊(duì)列
dispatch_queue_main_t
dispatch_get_main_queue(void)
{
return DISPATCH_GLOBAL_OBJECT(dispatch_queue_main_t, _dispatch_main_q);
}
_dispatch_main_q在libDispatch中定義
DISPATCH_GLOBAL_OBJECT的含義是類型轉(zhuǎn)化,dispatch_queue_main_t通過追蹤是拼接的一個(gè)類名OS_dispatch_queue_main
這個(gè)主隊(duì)列是什么時(shí)候創(chuàng)建的呢寝并?
在初始化的libdispatch_init中我們看到如下代碼
_dispatch_queue_set_current(&_dispatch_main_q);
_dispatch_queue_set_bound_thread(&_dispatch_main_q);
全局隊(duì)列
_dispatch_root_queues中保存了全局隊(duì)列箫措,所屬的類為OS_dispatch_queue_global,DISPATCH_GLOBAL_OBJECT_HEADER表示了對象所屬的類食茎,dq_serialnum顯示了并發(fā)蒂破。
創(chuàng)建的隊(duì)列
創(chuàng)建對象dispatch_queue_create=======>_dispatch_lane_create_with_target
dispatch_queue_create創(chuàng)建的隊(duì)列可能是并發(fā)或者串行的,通過下面代碼區(qū)分
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中確定并發(fā)和串行别渔,如果是并發(fā)則設(shè)置最大并發(fā)數(shù)DISPATCH_QUEUE_WIDTH_MAX = 4092附迷,串行則為1