Probe about GCD and my personal opinions (I hope the cool people will give me some advice to solve my confusion !), Gcd

Source: Internet
Author: User

Probe about GCD and my personal opinions (I hope the cool people will give me some advice to solve my confusion !), Gcd

For the differences between synchronous tasks and asynchronous tasks in GCD, previous generations thought that:

Later, some people updated the microblog short book, saying: the difference between synchronous tasks and asynchronous tasks is that the former will block the main thread and will not block the main thread when it is alive. I thought so at the beginning, but I was confused when I wrote the Demo. I don't know whether I understood it wrong or why, that is, I cannot explain it. The following is the exercise code:

- (void)testOne {        NSLog(@"testOne_start - %@",[NSThread currentThread]);        dispatch_queue_t queue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);        dispatch_sync(queue, ^{                NSLog(@"testOne_sync - %@",[NSThread currentThread]);    });        NSLog(@"testOne_end - %@",[NSThread currentThread]);}

The running result is as follows:

15:18:09. 024 GCD exercise [4033: 28550] testOne_start-<NSThread: 0x7fafe8506790> {number = 1, name = main}
15:18:09. 025 GCD exercise [4033: 28550] testOne_sync-<NSThread: 0x7fafe8506790> {number = 1, name = main}
15:18:09. 025 GCD exercise [4033: 28550] testOne_end-<NSThread: 0x7fafe8506790> {number = 1, name = main}

 

 

- (void)testTwo {        NSLog(@"testTwo_start - %@",[NSThread currentThread]);        dispatch_queue_t queue = dispatch_get_main_queue();        dispatch_sync(queue, ^{               NSLog(@"testTwo_sync - %@",[NSThread currentThread]);    });        NSLog(@"testTwo_end - %@",[NSThread currentThread]);}

The running result is as follows: testTwo_start-<NSThread: 0x7fc711f046e0> {number = 1, name = main

The two pieces of code are simple. The only difference is that the former adds the block to a newly created serial queue, while the latter adds the block to the main queue, the main queue is also a serial queue.

According to the predecessors, the synchronization task will block the main thread, and the latter can be clearly explained, but I have been unable to explain the former for a long time. The main thread is blocked. why can't the block tasks listed in the serial queue created by myself be executed, but the block tasks added to the main queue cannot be executed? If you don't understand, check the information! This is an explanation I have found about the serial queue: The serial dispatch queue can only execute one task at a time. After the current task is completed, columns are started and the next task is started. "The current task is completed to start the column and start the next task ". Therefore, I wonder if the synchronization task cannot be returned immediately (the block must be completed), and the next task cannot be executed in columns. Simply put, the synchronization task blocks the queue rather than the thread (adding the synchronization task to the main queue will cause the queue after the synchronization task to fail to be executed in the column, ). The following is my personal understanding of the above two pieces of code that won't lead to deadlocks and the latter will lead to deadlocks:

  • Tasks in the serial queue are executed one by one. The next task is listed only when the current task is completed.
    Here we create a serial queue and a synchronization task, and submit the synchronization task (Official Document: Submit a block like a queue, and this function will not return until the block is completed ), it is thrown into the serial queue. Program execution:
    1. The primary team columns the first NSLog;

    2. the synchronization task is then executed. The synchronization task creates a task and is thrown into the serial queue. The synchronization task is returned only after the block is executed. Therefore, the main queue considers that the synchronization task has not been completed, therefore, the third NSLog task cannot output columns. Continue to execute blocks (columns from serial) and return after the block synchronization task is executed.

    3. The third NSLog task is listed.

 

  • 1. The first NSLog task in the listing column is completed by the main team.

    2. the main team lists the second synchronization task in the column. The synchronization task creates a block task and adds it to the main column to wait for the block to be executed in the column, but the synchronization task must wait until the block is executed to return, however, for a serial Queue (main queue), only the previous task is completed and the next task can be columns. Therefore, the block task cannot be executed in columns, and the synchronization task can be executed out of Columns with blocks, causing a deadlock.

The above is my understanding and the only explanation I can give to the above two codes, however, after all, the opposite was mentioned in the previous article (when I checked the materials, I also found that some people thought it was a blocking queue, But I was refuted). I still hope that God will give me some advice, to solve my doubts.

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.