iOS学习笔记-死锁deadlock理解

1.首先看一下官方文档的解释,这个block的队列是同步执行的,不像异步,这个方法直到block执行完毕才会返回

2.主线程一旦开启,就要先把自己的代码执行完成之后,才去执行加入到主队列中的任务

Declaration

void dispatch_sync( dispatch_queue_t queue, dispatch_block_t block);

Parameters

queue

The queue on which to submit the block. This parameter cannot be NULL.

block

The block to be invoked on the target dispatch queue. This parameter cannot be NULL.

Discussion

Submits a block to a dispatch queue for synchronous execution. Unlike dispatch_async, this function does not return until the block has finished. Calling this function and targeting the current queue results in deadlock.

死锁原因:

a)       dispatch_sync这个方法要等到block的执行完之后,才返回

b)      主线程一旦开启,就要先把自己的代码执行完成之后,才去执行加入到主队列中的任务

c)       这样主线程想要执行block,先要下去执行下面的代码,但是因为dispatch_sync这个方法,,如果返回不了,主线程就无法向下再次执行,所以造成了死锁,所以把主线程卡到这里了,造成了死锁

 

posted @ 2016-01-21 08:24  Eric-bky  阅读(479)  评论(0编辑  收藏  举报