2014年11月19日 星期三

Objective-C 多線程: Grand Central Dispatch(GCD)

Serial sync嵌套會死鎖, 外部的sync block會等待內部的sync block完成才結束, 但內部的永遠執行不到
GCD_TEST[45149:303] HELLO
GCD_TEST[45149:1a03] async:{name = (null), num = 2}
-
Concurrent 怎麼嵌套都不會死鎖, 因為block沒有順序依賴關係
GCD_TEST[45167:303] HELLO
GCD_TEST[45167:303] sync:{name = (null), num = 1}
GCD_TEST[45167:1503] async:{name = (null), num = 2}
GCD_TEST[45167:1503] sync-async:{name = (null), num = 2}
GCD_TEST[45167:1a03] async-async:{name = (null), num = 3}
GCD_TEST[45167:303] sync-sync:{name = (null), num = 1}
GCD_TEST[45167:1503] sync-async:{name = (null), num = 2}
-
Main async 和 sync 相同, 都在主線程, 怎麼嵌套都會死鎖, 只用sync也無法執行, 因為main queue一直有任務在執行
GCD_TEST[45083:303] HELLO
-
Global 系統共用的queue, 效果同Concurrent
GCD_TEST[45187:303] HELLO
GCD_TEST[45187:303] sync:{name = (null), num = 1}
GCD_TEST[45187:1a03] async:{name = (null), num = 2}
GCD_TEST[45187:1a03] sync-async:{name = (null), num = 2}
GCD_TEST[45187:1f03] async-async:{name = (null), num = 3}
GCD_TEST[45187:303] sync-sync:{name = (null), num = 1}
GCD_TEST[45187:1a03] sync-async:{name = (null), num = 2}