Deadlock and deadlock

Source: Internet
Author: User

Deadlock and deadlock

Example: A standard deadlock

-(Void) viewDidLoad {[super viewDidLoad]; dispatch_sync (dispatch_get_main_queue (), ^ {});}

Dispatch_sync (queue, block) does two things

  • Add the block to the queue;
  • The call thread is blocked. Wait until the block () Execution ends and return to the call thread.

Dispatch_async (queue, block) also does two things:

  • Add the block to the queue;
  • Return directly to the calling thread (without blocking the calling thread ).

The difference between synchronous distribution and asynchronous distribution is to check whether the call thread is blocked.

 

We ignore that the main thread is a first-in-first-out (FIFO) method, while the viewdidload method belongs to the main thread. Therefore, the main thread should finish the viewdidload task first, then execute the next one, but if the synchronous execution is added to the main thread, viewdidload needs to be executed first, but viewdidload is waiting for the synchronization to complete, so the deadlock is solved.

 

So remember this lesson: do not distribute the block to the associated queue of the thread where GCD is called synchronously.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.