Thread Deadlock Issues

Source: Internet
Author: User

-(void) viewdidload {[Super viewdidload];    NSLog (@ "1");    Dispatch_sync (Dispatch_get_main_queue (), ^{NSLog (@ "2");    }); NSLog (@ "3");} What will it output and whyAnswer:Output 1,Use Dispatch_sync (Dispatch_get_main_queue () ^ () {block body}) in iOS;

Dispath adds a synchronized block to the home team;

At this point the home queue is waiting for Dispatch_sync (Dispatch_get_main_queue (), ^ () {block Body}), and the execution

The Dispatch_sync is waiting for the home queue to finish executing.

thereby causing a deadlock.

So you should use asynchronous execution when iOS uses the primary queue Dispatch_get_main_queue ()

Dispatch_Async(Dispatch_get_main_queue (), ^ () {

NSLog (@ "2");

});

Conclusion: Dispatch_sync

Will call the Dispatch_sync line routines and so on the contents of Dispatch_sync, and then continue to execute.

Dispatch_async

The thread that calls Dispatch_async does not have to wait for dispatch_async content to continue executing itself.

Dispatch_sync/async (Dispatch_get_main_queue (), ^ () {Block Body})

Just add the block to the queue. Sequence of execution in order of queue

The difference between sync/async is whether the thread that calls Diapatch waits for dispatch to finish executing.

Thread Deadlock Issues

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.