ios--some doubts about GCD

Source: Internet
Author: User

Explain GCD's information to see a lot, but did not solve the doubts of the mind:

1. What is the difference between synchronous and asynchronous execution if the home row has only one thread? Synchronizing the main thread will cause threads to deadlock, not async, why?

2. What is the difference between performing a serial queue synchronously and performing a parallel queue synchronously? Synchronous execution, which is the execution of tasks in the queue in the current thread, means that the tasks are executed in one thread, and it is not possible to perform multiple tasks at the same time.

3. Why does it not cause thread deadlock when the serial queue is executed synchronously in the main thread?

There is doubt, throat. There is doubt on behalf of the study is not in place, the concept of understanding is not enough. Therefore, I began to learn gcd, to clarify these basic concepts .

There are 4 terms that are more easily confused: synchronous, asynchronous, concurrent, serial

Synchronous and asynchronous determine whether to open a new thread

Synchronous: Performs a task in the current thread without the ability to open a new thread

Async: Perform a task in a new thread with the ability to open a new thread

Concurrency and serialization determine how tasks are executed

Concurrency: Multiple tasks concurrently (concurrently) executed

Serial: Once a task is completed, the next task is performed

Combine:

    1. Synchronous execution of Serial queues
    2. Synchronous execution of concurrent queues
    3. Asynchronously executes a serial queue
    4. Asynchronous execution of concurrent queues
    5. Synchronous execution of the home column
    6. Execute the home row asynchronously

I joined the home row (the serial queue of only one thread) because it differs from the normal serial queue. I'll analyze each of these 6 cases one by one.

1. Synchronous execution of Serial queues: Executes the tasks in the queue, in the current thread.

2. Synchronous execution of concurrent queues: Same as 1

3. Asynchronously executes a serial queue: Opens a new thread and executes the tasks in the queue one at a.

4. Asynchronously executes a concurrent queue: turns on multiple threads while performing multiple tasks in the queue.

First of all the four cases of nesting execution, in the main line of the two cases.

The following is a lot of code, the comparison between the code and print the results of thinking, to see if they do not understand that the explanation .

Demo Address: Https://github.com/jiliuliu/FoundationOC

intMainintargcConst Char*argv[]) {@autoreleasepool {dispatch_queue_t backgroundserialqueue= Dispatch_queue_create ("Backgroundserialqueue", dispatch_queue_serial); dispatch_queue_t Serialqueue= Dispatch_queue_create ("Serial", dispatch_queue_serial); dispatch_queue_t Concurrentqueue= Dispatch_queue_create ("Concurrent", dispatch_queue_concurrent); //avoid the main thread and open a single threaded testDispatch_async (Backgroundserialqueue, ^{NSLog (@"%@", [Nsthread CurrentThread]); Dispatch_sync (Serialqueue,^{[Nsthread sleepfortimeinterval:0.01]; NSLog (@"Dispatch_sync (serialqueue) 1");            }); Dispatch_sync (Serialqueue,^{[Nsthread sleepfortimeinterval:0.01]; NSLog (@"Dispatch_sync (serialqueue) 2");            }); Dispatch_sync (Serialqueue,^{[Nsthread sleepfortimeinterval:0.01]; NSLog (@"Dispatch_sync (serialqueue) 3");                        }); NSLog (@"************************"); Dispatch_sync (Concurrentqueue,^{[Nsthread sleepfortimeinterval:0.01]; NSLog (@"Dispatch_sync (concurrentqueue) 1");            }); Dispatch_sync (Concurrentqueue,^{[Nsthread sleepfortimeinterval:0.01]; NSLog (@"Dispatch_sync (concurrentqueue) 2");            }); Dispatch_sync (Concurrentqueue,^{[Nsthread sleepfortimeinterval:0.01]; NSLog (@"Dispatch_sync (concurrentqueue) 3");                    });    }); } NSLog (@"----Exit----"); return 0;}

Printing results:

0x10064f1b0 2, name = (null)}

The feeling and the anticipation are wrong, so modify the following:

intMainintargcConst Char*argv[]) {@autoreleasepool {dispatch_queue_t backgroundserialqueue= Dispatch_queue_create ("Backgroundserialqueue", dispatch_queue_serial); dispatch_semaphore_t Semaphore= Dispatch_semaphore_create (0); dispatch_queue_t Serialqueue= Dispatch_queue_create ("Serial", dispatch_queue_serial); dispatch_queue_t Concurrentqueue= Dispatch_queue_create ("Concurrent", dispatch_queue_concurrent); //avoid the main thread and open a single threaded testDispatch_async (Backgroundserialqueue, ^{NSLog (@"%@", [Nsthread CurrentThread]); Dispatch_sync (Serialqueue,^{[Nsthread sleepfortimeinterval:0.01]; NSLog (@"Dispatch_sync (serialqueue) 1");            }); NSLog (@"Dispatch_sync (serialqueue)"); Dispatch_sync (Serialqueue,^{[Nsthread sleepfortimeinterval:0.01]; NSLog (@"Dispatch_sync (serialqueue) 2");            }); NSLog (@"Dispatch_sync (serialqueue) 2--"); Dispatch_sync (Serialqueue,^{[Nsthread sleepfortimeinterval:0.01]; NSLog (@"Dispatch_sync (serialqueue) 3");            }); NSLog (@"Dispatch_sync (serialqueue) 3--"); NSLog (@"************************"); Dispatch_sync (Concurrentqueue,^{[Nsthread sleepfortimeinterval:0.01]; NSLog (@"Dispatch_sync (concurrentqueue) 1");            }); NSLog (@"Ispatch_sync (concurrentqueue)"); Dispatch_sync (Concurrentqueue,^{[Nsthread sleepfortimeinterval:0.01]; NSLog (@"Dispatch_sync (concurrentqueue) 2");            }); NSLog (@"Ispatch_sync (concurrentqueue) 2--"); Dispatch_sync (Concurrentqueue,^{[Nsthread sleepfortimeinterval:0.01]; NSLog (@"Dispatch_sync (concurrentqueue) 3");            }); NSLog (@"Ispatch_sync (concurrentqueue) 3--"); Dispatch_barrier_sync (Serialqueue,^{dispatch_semaphore_signal (semaphore);            }); Dispatch_barrier_sync (Concurrentqueue,^{dispatch_semaphore_signal (semaphore);        });        });        Dispatch_semaphore_wait (semaphore, dispatch_time_forever);    Dispatch_semaphore_wait (semaphore, dispatch_time_forever); } NSLog (@"----Exit----"); return 0;}

Printing results:

<nsthread:0x100600170>{number =2, name = (NULL)}dispatch_sync (serialqueue)1Dispatch_sync (serialqueue)1--Dispatch_sync (serialqueue)2Dispatch_sync (serialqueue)2--Dispatch_sync (serialqueue)3Dispatch_sync (serialqueue)3--************************Dispatch_sync (concurrentqueue)1Ispatch_sync (concurrentqueue)1--Dispatch_sync (concurrentqueue)2Ispatch_sync (concurrentqueue)2--Dispatch_sync (concurrentqueue)3Ispatch_sync (concurrentqueue)3------Exit----

Yes, that's the way it should be.

5. Synchronize the execution of the home column:

6. Execute the Home column asynchronously:

ios--some doubts about GCD

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.