iOS Multithreading Knowledge Summary

Source: Internet
Author: User

iOS Multithreading Knowledge Summary

1. Apple in iOS offers a 4-hour program to help us achieve multithreading:

(1) Pure C language Pthread, the lower level, the programmer needs to manually manage the life cycle of the thread, basically not.

(2) OC language Nstread, need to manually manage the thread life cycle, occasionally debug to get the current thread [Nstread Currenttread/maintread];

(3) Pure C language gcd (Grand Central dispatch Great Hub Scheduler), automatically manages the thread life cycle, completely hides the tread word, facing tasks and queues, has synchronous and asynchronous functions, automatically determines the number of threads opened, automatically manages the thread's life and death, Although it is a C language, it is often used.

(4) OC language nsoperation, the lower layer encapsulates the GCD, automatically manage the thread life cycle, more than gcd more features, because it is OC, more object-oriented, often used.

2. GCD Related summary:

2.1 It is best not to have more than 5 threads in iOS at the same time, GCD is also the same operation. You can use GCD to open 10 threads on the main thread, and then print each thread in the block, discovering that up to 5 threads in the primary queue execute at the same time. The GCD automatically determines how many threads are opened based on the number of cores (single, dual, quad) of the current device CPU.

2.2 GCD blocks threads, programmers don't have to pay attention to when the Dispath starts the thread, when to perform the task, and when to close the thread. GCD for tasks and queues, when using the GCD function, you only need to tell it the task (written in block) and queue name (queues) parameters, GCD will automatically add the task to the queue, and then follow the queue FIFO principle, take out the task, open the thread, Complete the task in the thread. GCD just silently in the thread to finish the task, done it will not feedback any hint message.

2.3 Commonly used 2 simplest GCD functions:

Dispatch_sync (dispatch_queue_t queue, ^ (void) block); Sync synchronization, the task block in the queue can only be executed concurrently/serially in the current thread (usually the main thread/ui threads), and the new thread cannot be opened to perform the tasks in the queue.

  Dispatch_async (dispatch_queue_t queue, ^ (void) block); Async async, the ability to open multiple threads (typically up to 5) concurrent/serial execution of the block task in the queue.
2.4 About serial and concurrency:

These two concepts are for queue queues, there will be a home column in each thread, or you can create a new queue yourself, when using the dispatch function, the passed parameter is the queue name and block, then the block will be added to the queue waiting to be executed. A serial queue is one task that can only be taken out of a queue at a time, and the preceding is not executed. A concurrent queue is one that can remove multiple tasks from a queue at a time, with multiple threads executing simultaneously. The subsequent task may be executed at the same time as the previous task. Concurrent queues can only be executed in the asynchronous GCD function Dispatch_async.

2.5 Easy to confuse terminology:

Synchronous/asynchronous for GCD functions, the main impact can not open multiple threads, serial and concurrent for queue queues, the main impact of the task's execution methods.

Synchronization: The GCD function can only be executed on the current thread and cannot open a new thread.

Async: The GCD function can open multiple threads to execute tasks in a queue.

Serial: Only one task can be fetched at a time in the queue and execution is done before the next.

Concurrency: In a queue, you can remove multiple tasks at once and execute them concurrently in multiple threads.

2.6 gcd Most common usage: Global concurrent queues Perform tasks asynchronously

GCD will automatically help us create a global concurrency queue that the entire program can access, we get it anywhere in the program (), and then add the task with gcd async function Dispatch_async, with GCD automatically opening threads in the background and executing concurrently.

2.7 Main_queue and Global_queue differences

NSLog(@ "main:%@, global:%@",dispatch_get_main_queue(),dispatch_get_global_queue( dispatch_queue_priority_default, 0));

Queue priority is:Default/high/low , etc., generally with DEFAULT. 0 is a reserved parameter.

Printing results:

Main: <os_dispatch_queue:com.apple.main-thread[0x10fb229c0]>

Global:<os_dispatch_queue_root:com.apple.root.default-qos[0x10fb23240]>

iOS Multithreading Knowledge Summary

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.