iOS dispatch

51 阅读1分钟

如果在主线程执行以下代码:

dispatch_sync(dispatch_get_main_queue(), block());

等价于:

step1. queues the block in the main queue.

step2. blocks the thread of the main queue until the block finishes executing.

step3. waits forever because the thread where the block is supposed to run is blocked.

dispatch_sync并不执行block,仅为其排队

if (queueA == dispatch_get_current_queue()){
    block();
} else {
    dispatch_sync(queueA, block);
}