GCD thread deadlock

Source: Internet
Author: User

GCD thread deadlock

GCD is really easy to use and powerful. Compared with NSOpretion, GCD cannot cancel tasks.

If such a powerful tool is used badly, a thread deadlock may occur. The following code:

- (void)viewDidLoad{    [super viewDidLoad];    NSLog(@"=================4");    dispatch_sync(dispatch_get_main_queue(), ^{        NSLog(@"=================5");    });    NSLog(@"=================6");}

There are three types of GCD Queue:

1,The main queue: The main queue column. The main thread is in a queue.

2,Global queues: Global concurrency queue.

3,User queue: Use a functiondispatch_queue_createCustom queue created

 

Difference between dispatch_sync and dispatch_async:

Dispatch_async (queue, block) async asynchronous queue,dispatch_asyncThe function returns immediately, and the block is asynchronously executed in the background.

Dispatch_sync (queue, block) sync synchronization queue,dispatch_syncThe function does not return immediately and blocks the current thread, waiting for the block to be executed synchronously.

 

Analyze the code above:

ViewDidLoad in the main thread, and in
In dispatch_get_main_queue (),
Dispatch_get_main_queue () insert synchronization threed1.

Sync will not return until the subsequent block execution is complete, and sync will be in the dispatch_get_main_queue () queue again,
It is a serial queue, sync is followed, and the first is the main thread,
Therefore, to execute a block in sync, you must wait for the main thread to complete the execution, and the main thread to wait for the sync return to execute subsequent content.

As a deadlock occurs, sync waits for mainThread execution to complete, and mianThread waits for the sync function to return.

The following example:
- (void)viewDidLoad{    [super viewDidLoad];    dispatch_async(dispatch_get_global_queue(0, 0), ^{        NSLog(@"=================1");        dispatch_sync(dispatch_get_main_queue(), ^{        NSLog(@"=================2");    });    NSLog(@"=================3");    });
}

 

Why is there no deadlock when the program is executed successfully.

First, async creates an asynchronous thread in the main thread to join the global concurrent queue. async does not wait until the block execution is complete and returns immediately,

1. async returns immediately, viewDidLoad is executed, and the master thread is executed.
2. At the same time, the global concurrent queue immediately executes asynchronous block and prints 1. When it is executed to sync, it will wait until the block execution is complete before returning and waiting.
The execution of mianThread in the dispatch_get_main_queue () queue is complete before calling the block.

Because 1 and 2 are executed almost simultaneously, because 2 is in the global concurrency queue, when 2 is executed to sync, 1 may have been executed successfully or waited for a while, mainThread exited quickly, 2. Subsequent content has been executed.

If the main thread is blocked, the sync in step 2 will not be executed, the mainThread will never exit, and the sync will always wait,

-(Void) viewDidLoad {[super viewDidLoad];
Dispatch_async (dispatch_get_global_queue (0, 0), ^ {NSLog (@ "================== 1 "); dispatch_sync (dispatch_get_main_queue (), ^ {NSLog (@ "================ 2 ");}); NSLog (@ "=========================== 3 ");}); NSLog (@ "=========== blocking the main thread"); while (1) {} NSLog (@ "=========== 2 === blocking the main thread ");}

Print as follows:

17:56:22. 296 Test [6108: 379350] ==================== 1

17:56:22. 296 Test [6108: 379231] ========= blocking the main thread

Always wait .....

Knowing the principles will not cause errors.

 

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.