主進(jìn)程中GCD用main queue會(huì)卡死但用新生成串行queue卻沒問題

主進(jìn)程中添加任務(wù)到GCD的main?queue會(huì)卡死,而如果用生成的queue卻正常運(yùn)行:

dispatch_queue_t?serialQueue?=?dispatch_queue_create("squeue",NULL);

//????dispatch_queue_t?mainQueue?=?dispatch_get_main_queue();

NSLog(@"Main?Queue-->%@",?dispatch_get_main_queue());

dispatch_sync(serialQueue,?^{//如果用mainQueue就卡死

NSLog(@"Task?1-->%@",?[NSThreadcurrentThread]);

});

在main()中執(zhí)行沒問題,如果dispatch_sync改用mainQueue的話就卡死了。


這個(gè)是最典型的互相等待導(dǎo)致鎖死呀。

調(diào)用的dispatch_sync函數(shù)一直得不到返回,main?queue被阻塞,而我們的block又需要等待main?queue來執(zhí)行它,死鎖愉快的產(chǎn)生了。


1

Everything?in?your?program?runs?on?the?main?queue?except?for?code?that?you?explicitly?dispatch?onto?another?queue.?Code?on?the?main?queue?will?always?run?on?the?main?thread.?Code?dispatched?on?another?queue?may?or?may?not?run?on?the?main?thread.

當(dāng)然也可能是自己太菜,連所有語句都是運(yùn)行在主隊(duì)列上這種大家都普遍默認(rèn)的常識(shí)都不懂,所以才會(huì)大費(fèi)周章的解釋死鎖現(xiàn)象。因?yàn)橐郧坝胏,不太有iOS主隊(duì)列的概念。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容