Multithreading in IOS development and multithreading in ios

Source: Internet
Author: User

Multithreading in IOS development and multithreading in ios

This blog post on GCD reads many articles about GCD by great gods at home and abroad, as well as a rough understanding of GCD, and then uses its essence to get rid of it, the integrated notes are presented to you in a familiar and understandable way as much as possible and are correctly discussed in theory. We have already tried our best to give you the knowledge of multithreading and the use technology of GCD. In the final appendix, I will provide links to all articles written by my reading experts on multithreading or GCD. If you are interested, you can refer to and learn.

This blog post: Continuous updates are not complete yet.

 

Basic Concepts

1. What is GCD?

The iOS implementation provides the following multithreading solutions: NSThread, NSOperation, NSInvocationOperation, pthread, and GCD.

Among all iOS multi-thread Implementation Solutions, GCD should be the most attractive and convenient to use, because GCD is Apple's solution for multi-core parallel computing.

GCD is short for Grand Central Dispatch, which is based on the C language. When using GCD, we do not need to write thread code, nor do we need to manually manage its lifecycle, define the task to be executed, and add it to the appropriate scheduling queue, that is, dispatch queue. GCD is responsible for creating threads and scheduling tasks, and the system provides thread management directly.

 

2. Task)

A piece of code block that can be implemented after execution. A closure or block is generally used to include it. To execute a task, you need to hand over the closure or block to the queue, and then let the queue first-in-first-out (FIFO) Order, and put it into the thread for execution.

 

3. Queue)

We need to understand the concept of queues. GCD provides dispatch queues to process code blocks. These queues manage the tasks provided to GCD and execute these tasks in FIFO order. In this way, the first task to be added to the queue will be the first task in the queue, and the second task to be added will start the second until the end of the queue.

  • Serial queue

    A task in a serial queue is obtained in the first-in-first-out (FIFO) Order Defined by the queue. Execute one task, complete the execution, and then extract the next task. In this way, the task is executed one by one.

    Serial queue diagram:

   

  • Concurrent queue

    Tasks in a concurrent queue are also retrieved one by one based on the sequence of first-in-first-out (FIFO) defined in the queue. However, unlike a serial queue, the concurrent queue extracts a task and puts it in another thread for execution. no matter whether the thread is a synchronous thread or an asynchronous thread, the concurrent queue does not finish the task when the task in the thread is finished, the next task from the concurrent queue will be immediately put into another thread for execution. Because the task retrieval speed is fast and negligible, it seems that all tasks start to execute at the same time. However, the task is long and short, and you cannot determine the sequence in which the task is executed. It is possible that the shortest task is executed first.

    Concurrency queue diagram:

   

Supplement: different parts of the concurrent code can be executed synchronously. However, how or whether it happens depends on the system. Multi-core devices execute multiple threads simultaneously in parallel. However, to enable single-core devices to achieve this, they must first run a thread and execute a context switch, then run another thread or process. This usually takes place fast enough to give us the illusion of concurrent execution, as shown in:

  

 

4. Queue type (Queue Types)

Provided by the system:

  • A main queue is a serial queue.

    Like other serial queues, only one task in this queue can be executed at a time. However, it ensures that all tasks are executed in the main thread, and the main thread is the only thread that can be used to update the UI. This queue is used to send messages to UIView or send notifications.

When using the serial queue of the main thread, we can only obtain it through the dispatch_get_main_queue method. We do not need to manage its memory problems.

  • All four Global scheduling Queues (Global Dispatch Queues) are parallel Queues.

    The four global queues have different priorities: background, low, default, and high. You know, Apple APIS also use these queues, so any task you add will not be the only task in these queues.

When using the Global concurrency queue, we can only obtain it through the dispatch_get_global_queue method, and we do not need to manage its reference.

  • You can also create your own serial queue or concurrent queue.

Conclusion: there are at least five queues for your disposal: the main queue, four global scheduling queues, and any queue created by yourself.

The above is a big framework of scheduling queues!

 

5. threads

There are two types of threads: Synchronous vs. Asynchronous Synchronous vs. Asynchronous.

How to Create a synchronous thread and an asynchronous thread in GCD:

  

There are two things to say: Synchronous function and asynchronous function. We have learned about the Function in C language. The function contains a block of code in braces. After the function is executed, it will return void or a specific data value, it also indicates that the execution of this function is complete. The function in the thread includes the task, which is a closure or block. The difference between a synchronous function and an asynchronous function is that a synchronous function is returned only after it has completed its scheduled task, and an asynchronous function returns immediately, that is to say, the scheduled task of the asynchronous function will be completed but will not be returned immediately after it is completed.

In addition, the synchronization thread does not have the ability to create sub-threads. Generally, tasks of the synchronization thread are executed in the unique main thread, that is, the unique (main) the main thread becomes a synchronization thread. Asynchronous threads can be used to create subthreads. Generally, asynchronous threads are threads other than the main thread. They are not unique and have multiple threads, the specific threads, if created through GCD, are all determined by GCD. The maximum number of sub-threads that can be created depends on the CPU capacity of the device. For example, I used a real machine to create multiple asynchronous tasks by using GCD, and add the result to the concurrent queue for testing:

  

We can see that I have created eight asynchronous tasks. Based on the device's limits, GCD has created three sub-threads: thread 2, thread 3, and thread 4, which then run the tasks randomly. Thread 4 executes four tasks, thread 2 and thread 3 respectively.

So, synchronization is asynchronous. The two terms need to be compared between multiple function tasks, which will be better understood. For example, during synchronization, when a thread executes a synchronous function task, the thread stops, that is, the so-called "the current thread is blocked, the current thread needs to wait for the function to be executed to return a value, indicating that the task in the function has been fully executed. This thread will continue to execute the next function task, so such a thread is also calledSynchronization thread. The threads where asynchronous functions are located are different. When a thread starts to execute an asynchronous function task, the asynchronous function returns immediately. Although this function task may not be completed yet, however, if it is returned, this thread will continue to execute the next function task. Because the process is very fast, it is almost possible to ignore the order in which the task execution starts, and it seems that all tasks start at the same time, the so-called"Asynchronous threadExecute multiple tasks at the same time.

 

Note:

Do not place time-consuming operations in the main thread. All operations related to the UI are processed in the main thread.

Time-consuming operations should be placed in sub-threads (background threads, non-main threads)

 

6. Summarize the use of queues and threads at the same time.

Well, let's take a look at this figure. It may be a little different from what you may look at elsewhere. I have modified it based on the above theory:

  

6-1. Serial queue + synchronization:

Assume that a bunch of tasks are placed in a serial queue.

Because the serial queue is a first-in-first-out/first-out queue, and the serial queue is a task that needs to be taken out before it is executed.

Because synchronization (sync) does not enable the new thread.

Therefore, this heap of tasks will be executed in the main thread. After the execution is completed, the next task starts and continues until the execution is completed.

The current thread is blocked.

Verification Code:

  

Print result:

15:51:04. 970 multithreading [25106: 632356] The current thread is: <NSThread: 0x7fe28bc025b0 >{ number = 1, name = main}
15:51:06. 974 multithreading [25106: 632356] The current thread is: <NSThread: 0x7fe28bc025b0 >{ number = 1, name = main}
15:51:08. 979 multithreading [25106: 632356] The current thread is: <NSThread: 0x7fe28bc025b0 >{ number = 1, name = main}
15:51:10. 980 multithreading [25106: 632356] The current thread is: <NSThread: 0x7fe28bc025b0 >{ number = 1, name = main}

6-2. Concurrent queue + asynchronous:

 

6-3,

6-4,

 

 

 

Appendix: Good GCD learning webpage links

1, GCD this has been open source, address http://libdispatch.macosforge.org

2, Tang Qiao technology blog: "Use GCD", address: http://blog.devtang.com/2012/02/22/use-gcd/

3, great god translation from foreign IOS very good learning site article "Grand Central Dispatch In-Depth: Part 1/2": https://github.com/nixzhu/dev-blog

4, the standard brother technology blog: "GCD from simple to deep learning": http://www.henishuo.com/gcd-multiple-thread-learn/

5. Tutu brother's "GCD experience and skills": http://tutuge.me/2015/04/03/something-about-gcd/

6. Grand Central Dispatch (GCD) Reference by YouXianMing: http://www.cnblogs.com/YouXianMing/p/3600763.html

7, the IOS multithreading, you see me enough: http://www.jianshu.com/p/0b0d9b1f1f19

8. elaborate on how GCD (Grand Central Dispatch) is used: http://www.jianshu.com/p/fbe6a654604c

 

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.