Objective-c iOS multi-thread gcd deep understanding

Source: Internet
Author: User
Tags gcd

In GCD, two very important concepts were added: tasks and queues
A thread can have more than one execution queue, and all tasks are added to the queue waiting to be executed
The primary queue is a special serial queue, and the queue that you create can specify serial or parallel, and the global queue is a parallel queue

Task: namely operation, what you want to do, plainly is a piece of code, in GCD is a Block, so add the task is very convenient.
There are two ways to execute a task: synchronous execution and asynchronous execution, the main difference between them is whether the current thread will be blocked

First look at the following two examples:

1. dispatch_queue_t queue = dispatch_queue_create ("queue", dispatch_queue_serial);
Dispatch_sync (Queue, ^{
NSLog (@ "%d", [[Nsthread CurrentThread] ismainthread]);
});

2. dispatch_queue_t queue = dispatch_queue_create ("queue", dispatch_queue_serial);
Dispatch_async (Queue, ^{
NSLog (@ "%d", [[Nsthread CurrentThread] ismainthread]);
});


Two examples are the creation of a new queue, the only difference between the first is to run synchronously, the latter is asynchronous
As a result, the former prints 1, while the latter is 0.

We analyze that the meaning of synchronization here is actually for all execution queues of threads, that is, when synchronous execution, in addition to the block task, all other queues executed on the same thread are all suspended, and when the block task executes, the other queue tasks resume execution. ( here is a special note that the system in determining whether to suspend the execution of the queue, whether the block task is in the queue header, if not stop the queue execution )

In Example 1 above, creating a new queue, the block task at the first, the queue header, to synchronize execution is all the queues executed on the main thread are paused (except for the queue where the block task is located). Example 2 because a new queue is created and executed asynchronously, a new thread is created, so the print 0, which is the queue on the main thread, is executed as usual.

To prove my hypothesis above, I give an example:
3, dispatch_queue_t queue = Dispatch_get_main_queue ();
Dispatch_sync (Queue, ^{
NSLog (@ "%d", [[Nsthread CurrentThread] ismainthread]);
});

4, dispatch_queue_t queue = Dispatch_get_main_queue ();
Dispatch_async (Queue, ^{
NSLog (@ "%d", [[Nsthread CurrentThread] ismainthread]);
});


The above two examples are to add the block task to the main queue, but the result is different, example 3 result is never execute the PRINT statement, the program will no longer execute, example 4 normal execution, and in the main queue
Conclusion: A new thread must be turned on without being asynchronous

Example 3 because the block task is added to the main queue, the home column already has a task (the current statement is not finished, so the block task is not the task to be performed), according to the previous system determined that the host column is suspended, the previous task cannot be completed, the subsequent block task will not be completed, Cause a dead loop
Example 4 adds a task to the primary queue, although it is an asynchronous operation, but does not open a new thread because in the primary queue, the queue executes as it does in the main thread, and the asynchronous operation does not block the thread.

Conclusion: Synchronization will let the system decide to suspend execution of all queues that do not take the block task as the first task (that is, if the Block block task is not in the queue header, then the queue for the block task will also be paused), and the synchronization will not open the new thread, because GCD thinks that since the other queues are paused, Block blocks can be executed on the current thread, and it is not necessary to open a new thread. Asynchronously, the system does not determine, but does not necessarily open a new thread, which is related to the queue, if it is a newly created queue, then GCD will open a new thread, if you join the existing queue, then the queue is in the same thread to execute.

In a nutshell, the Block block task executes in the current thread, such as synchronizing in the main thread above, so it must be done in the main thread, regardless of whether the queue is a home column or a global queue or a queue created by itself. If it is an asynchronous operation, then it is necessary to look at the situation, if it is the home row, then it is executed in the main thread, if it is a global or a queue created by itself, then in the newly created thread, the global queue will automatically create one or more threads according to the task. The queues that you create are assigned according to the parameter settings and tasks.


As for serial and parallel, serial and parallel are for tasks in the same queue, when using serial, then the queue in the queue of the task can only use one thread to run, that is, at the same time only one task is executing, if it is parallel, then the system will automatically assign thread execution according to the task in the queue, Maximum number of threads set according to parameters

Reprint Please specify: Author Smithjackyson

Objective-c iOS multi-thread gcd deep understanding

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.