iOS Multithreading knowledge

Source: Internet
Author: User

One: process

The In-progress program is called a process, which is responsible for the memory allocation run by the program each process has its own independent virtual memory space

  Thread

A thread is a separate execution path (control unit) in a process that contains at least one thread in a process, where the master thread can place a time-consuming execution path (such as a network request) on another thread in order to open a new execution path, run the specified code, and run concurrently with code implementation in the main thread

  Advantage

Give full play to the multi-core processor advantages, assign different thread tasks to different processors, really enter the "parallel operation" state to assign time-consuming tasks to other threads to execute, the unified update interface from the main thread will make the application smoother, the user experience better when the number of hardware processors increases, the program runs faster, And the program does not need to make any adjustments

  cons new threads consume memory space and CPU time, and too many threads can degrade system performance

  misunderstanding multithreading technology is to execute multiple tasks concurrently, does not improve the efficiency of the execution of the individual algorithm itself

Note: Several ways to implement multithreading, in previous essays

Second: Thebasic idea of GCD

  1) The basic idea of GCD is to put the operation s in the queue s to perform operations using the blocks definition queue responsible for scheduling task execution of the thread and the specific execution time queue is characterized by FIFO (FIFO), the newly added to the column operation will be ranked at the end of the team

2) The function that prompts GCD is the queue dispatch_queue_t serial queue that starts with dispatch (Dispatch, dispatch), the task in the queue executes the parallel queue only sequentially, the task in the queue usually executes concurrently

3) Operation Dispatch_async Asynchronous operation, will be executed concurrently, unable to determine the order of execution of the task Dispatch_sync synchronous operation, will be executed sequentially, can determine the order of execution of the task

Three: How the GCD internal implementation (brief)

the core of 1> IOS and OS X is the XNU kernel,GCD is based on XNU Kernel-Implemented

2> GCD APIs are all in the Libdispatch library

the underlying implementations of the 3> GCD are mainly Dispatch queue and Dispatch Source

Dispatch Queue: Managing block ( operations )

Dispatch Source: Handling Events

Four: operation

Results of nesting dispatch_sync in different queues

  The global queue, which is executed on the main thread, does not deadlock dispatch_queue_t q = dispatch_get_global_queue (dispatch_queue_priority_default, 0);

Parallel queues, which are executed on the main thread, do not deadlock dispatch_queue_t q = dispatch_queue_create ("Cn.itcast.gcddemo", dispatch_queue_concurrent);

Serial queue, deadlock, but the code before the nested synchronization operation is executed

dispatch_queue_t q = dispatch_queue_create ("Cn.itcast.gcddemo", dispatch_queue_serial);

Direct deadlock

dispatch_queue_t q = Dispatch_get_main_queue ();

Dispatch_sync (q, ^{

NSLog (@ "Sync task%@", [Nsthread CurrentThread]);

Dispatch_sync (q, ^{NSLog (@ "Sync task%@", [Nsthread CurrentThread]);

});

});

V:gcd Queue

Summarize:

    • Serial queue, synchronous task, no new thread required
    • Serial queue, asynchronous task, requires a child thread, thread creation and recycling does not require programmer involvement! "Is the safest one to choose" Serial queue can only be created!
    • Parallel queue, synchronous task, no need to create thread
    • Parallel queue, asynchronous task, number of tasks, open n Threads execution
    • No matter what the queue and what tasks, the creation and recycling of threads does not require programmer involvement. The creation of a thread is handled by the queue
    • "Concurrent" programming, in order to let programmers from the responsible line program control to free out! Only need to face queues and tasks!
    • GCD manages a thread pool at the backend, gcd not only determines which thread the code block will be executed on, but also manages those threads based on available system resources. Mitigate problems created by a large number of threads by centralizing management threads

    • With GCD, developers can think of work as a queue rather than a bunch of threads, a parallel, abstract model that is easier to master and use

Tip: It is not recommended to use a queue of different priorities, because if poorly designed, a priority reversal may occur, that is, low-priority operations block high-priority operations

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.