Objective-c ios multi-thread GCD deep understanding, objective-cgcd

Source: Internet
Author: User

Objective-c ios multi-thread GCD deep understanding, objective-cgcd

Two important concepts are added to GCD: task and queue.
A thread can have multiple execution queues, and all tasks are added to the queue waiting for execution.
The main queue column is a special serial queue. You can specify serial or parallel for the self-created queue. The Global queue is a parallel queue.

Task: An operation. What do you want? To put it bluntly, it is a piece of code and a Block in GCD. Therefore, it is very convenient to add tasks.
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, let's look at the two examples below:

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]);});

In both cases, a new queue is created. The difference is that the former runs synchronously and the latter runs asynchronously.
The result is 1 for the former and 0 for the latter.

After analysis, synchronization means that all the execution queues of the thread are paused, that is, all the other queues executed in the same thread except the block task during synchronous execution, after the block task is executed, other queue tasks are resumed. (Note that the system determines whether to suspend the execution of the queue according to whether the block task is in the queue header. If not, stop the queue execution)

In example 1, a new queue is created, and the block task is in the first queue header, during synchronous execution, all queues executed in the main thread will be paused (except the queue where the block task is located ). Example 2: because a new queue is created and asynchronous execution is performed, a new thread is created for execution, so the printed 0, that is, the queue on the main thread is executed as usual.

To prove my above assumptions, Let me 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]);});

In the above two examples, the block task is added to the main queue, but the results are different. In Example 3, the print statement is never executed, and the program is no longer executed. Example 4 is executed normally, and in the main team Column
Conclusion: A New thread will be enabled if it is not asynchronous.

Example 3 because the block task is added to the main queue column, a task is already in the main queue column (the current statement is not completed, so the block task is not the task to be executed ), according to the previous system decision, the execution of the main queue is suspended, and the previous task cannot be completed, and the subsequent block task cannot be completed, resulting in an endless loop.
In Example 4, a task is added to the main queue. Although asynchronous operations are performed, new threads are not enabled, because in the main queue, tasks must be executed in the main thread, asynchronous operations do not block threads, so the queue is executed as usual.

Conclusion: synchronization will cause the system to suspend the execution of all queues that do not take block tasks as the first task (that is, if the block task is not in the queue header, the queue where the block task is located will also suspend execution), and the synchronization will not start a new thread, because GCD thinks that since other queues are suspended for execution, the block task can be executed in the current thread, and there is no need to start a new thread. While asynchronous mode does not determine, but does not necessarily enable the new thread. This is related to the queue. If it is a newly created queue, GCD will start the new thread, if an existing queue is added, it is executed in the thread where the queue is located.

To put it simply, the block task will be executed in the current thread during the synchronization operation. For example, if the block task is synchronized in the main thread, it must be executed in the main thread, whether the queue is a main queue, a global queue, or a self-created queue. If it is an asynchronous operation, it depends on the situation. If it is a main queue, it is executed in the main thread. If it is a global queue or a self-created queue, it is executed in the newly created thread. The Global queue automatically creates one or more threads Based on the task. The self-created queue is allocated according to parameter settings and tasks.


As for serial and parallel, serial and parallel are for tasks in the same queue. When serial is used, tasks in the queue can run only one thread at most, that is, only one task is being executed at the same time. If it is in parallel, the system will automatically allocate threads for execution according to the tasks in the queue. the maximum number of threads and data parameters are set.

 

 

Reprinted Please note: Author SmithJackyson

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.