Parallel code in iOS

Source: Internet
Author: User

With the development of technology, more and more mobile phone systems are equipped with multi-core processors. If you want to take advantage of its multi-core advantages in these systems to make your program run faster, you will inevitably need to use multithreading technology. More commonly, if we want to keep a long operation from getting stuck on the user interface, we usually hand over this operation to another thread. Of course, from the operating system perspective, this method is (almost) the only way, with no errors.

 


However, the operating system's understanding of threads is different from that of coders. The scheduling capability of the operating system for threads is beyond the capability of common code. This is shown in several aspects: first, it is difficult for programmers to dynamically adapt to their own number of threads based on the number of cores. Second: it is difficult for programmers to dynamically adapt to their thread priorities based on the core load. The best way to manage multiple threads is the operating system itself. These are all difficulties in writing high-quality multi-threaded code.

 


In the face of this situation, iOS provides a very good solution. GCD and NSOperationQueue are like this. There are several advantages when using Dispatch Queue and Operation Queue

1. reduces your spending on user program management threads

2. You do not need to write the code related to thread creation.

3. You do not need to write code related to job scheduling.

4. In general, your code is simplified.

 


Take Dispatch Queue as an Example


Void dispatch_async (dispatch_queue_t queue, dispatch_block_t block );

It allows the program to execute the code in the block on another thread and ensures that the execution sequence of the code you submit to the queue is not disrupted. In general, a single task thread in multi-threaded programming can be replaced by this. IOS provides four global parallel queues and the current non-parallel queue columns. We can also create parallel or non-parallel Queues with various priorities. With dispatch_async and dispatch_sync, we can select the queue in which our code is executed. You only need to make a little arrangement to complete the work that requires a lot of native thread programming code.

 


Similarly, NSOperationQueue can provide similar functions. However, in terms of interfaces, GCD is closer to the original thread than Operation Queue, and they provide more functions than GCD.

 


Of course, parallel processing is not only possible with multithreading. A simple example is that during this time when I translated the Android code to iOS, I encountered a lot of multi-threaded code. These multi-threaded codes are related to network interaction. The socket provided by iOS is a runloop-based parallel socket, in this way, in the runloop of every main thread, we can judge the next action of the program based on the received package and program status, instead, you don't need to give it to a subthread. In fact, if I want to talk about the advantages and disadvantages of multithreading and runloop, I dare not say anything. Because the error rate and running efficiency of each method are also related to the programmer's own way of thinking and programming habits. In principle, GCD is optimal. However, if you choose between common thread code and runloop code, I prefer runloop.

 


Of course, there are other cases where NSOperationQueue and GCD cannot be used. For the former, the pure C code or C ++ code is useless. For the latter, if your code is C ++ code and you need to be able to run directly on iOS and Android platforms (just like a lot of OpenGL Code ), in this case, parallel programming can only honestly write multiple threads. Tragedy

 

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.