Multi-thread and network development for iOS overview, multi-thread development for ios Overview
Master haomeng is devoted to his contribution and respects the author's Labor achievements. Do not repost them.
If the article is helpful to you, you are welcome to donate to the author, support haomeng master, the amount of donation is free, focusing on your mind ^_^
I want to donate: Click to donate
Cocos2d-X source code download: point I send
Game official download: http://dwz.cn/RwTjl
Game video preview: http://dwz.cn/RzHHd
Game Development blog: http://dwz.cn/RzJzI
Game source code transfer: Http://dwz.cn/Nret1
A. What is A process?
A process is an application running in the system. Each process is independent of each other. Each process runs in its dedicated and protected memory, such as opening QQ and Xcode at the same time, the system starts two processes separately. You can use the "activity monitor" to view the processes enabled in the Mac system. the memory occupied by the main thread and sub-thread is 1 MB and K1 respectively. what is thread
To execute a task, a process must have a thread (each process must have at least one thread). The thread is the basic execution unit of the process, and a process (Program) all tasks are executed in the thread. For example, playing music with a cool dog and downloading movies with thunder require 2. thread serial: the execution of tasks in one thread is serial.
If you want to execute multiple tasks in one thread, you can only execute these tasks one by one in order, that is, within the same time, one thread can only execute one task. For example, three files (File A, file B, and file C) can be downloaded in one thread. multithreading what is Multithreading
Multiple Threads can be enabled in one process, and each thread can execute different tasks in parallel (at the same time ).
In the Process Workshop, the thread workshop's multi-thread technology can improve the program execution efficiency, for example, enabling three threads to download three files (File A, file B, and file C) at the same time. multithreading principles multithreading principles
At the same time, the CPU can only process one thread, and only one thread is working (executed)
Multi-thread concurrent (concurrent) execution is actually a fast CPU scheduling (switching) between multiple threads)
If the CPU scheduling thread time is fast enough, it creates the illusion of multi-thread concurrent execution.
Think: what happens if there are many threads?
The CPU will be scheduled between N threads, and the CPU will be exhausted, consuming a lot of CPU resources
The frequency of scheduled execution of each thread is reduced (the thread execution efficiency is reduced) 5. Advantages and Disadvantages of Multithreading
Appropriately improves program execution efficiency
Resource utilization can be appropriately improved (CPU and memory usage)
Disadvantages of Multithreading
To enable a thread, a certain amount of memory space is required (by default, the main thread occupies 1 MB, and the sub-thread occupies KB). If a large number of threads are enabled, a large amount of memory space is occupied, program Performance reduction
The more threads, the higher the CPU overhead on the scheduling thread.
Program Design is more complex: for example, communication between threads and data sharing among multiple threads 6. What is the main thread?
After an iOS program runs, one thread is enabled by default, which is called "main thread" or "UI thread"
Main functions of the main thread
Display \ refresh UI
Process UI events (such as click events, rolling events, and drag-and-drop events)
Usage of the main thread
Do not place time-consuming operations in the main thread. Time-consuming operations will get stuck in the main thread, seriously affecting the smoothness of the UI, giving users a "card" bad experience 7. iOS multithreading Implementation Technology
- Pthread
- NSThread
- GCD
- NSOperation
C. multithreading to be mastered
1. NSThread
1> several methods of thread opening
* First create and then start
NSThread * thread = [[NSThread alloc] initWithTarget: self selector: @ selector (run) object: nil];
[Thread start];
* Start directly
[NSThread detachNewThreadSelector: @ selector (run) toTarget: self withObject: nil];
[Self defined mselectorinbackground: @ selector (run) withObject: nil];
2> other usage
NSThread * current = [NSThread currentThread];
+ (NSThread *) mainThread; // obtain the main thread
3> inter-thread Communication
Performselecdomainmainthread .....
2. GCD (important)
1> queue type
* Concurrent queue
Obtain the global concurrency queue: dispatch_get_global_queue
* Serial queue
A. Create your own
Dispatch_queue_create
B. Main Columns
Dispatch_get_main_queue
2> method type of task execution
* Sync execution
* Asynchronous (async) Execution
3> understand how queues and methods work together
4> inter-thread Communication
Dispatch_async (
Dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
// Execute time-consuming asynchronous operations...
Dispatch_async (dispatch_get_main_queue (), ^ {
// Return to the main thread and perform the UI refresh operation
});
});
5> other usage
Dispatch_once
Dispatch_after
Dispatch_group_async \ dispatch_group_policy
3. NSOperation
1> basic usage
NSInvocationOperation
NSBlockOperation
2> NSOperationQueue (important)
* Set the maximum number of concurrent threads-(void) setMaxConcurrentOperationCount :( NSInteger) cnt; Note: This method can only limit the number of concurrent threads, you cannot restrict specific threads (that is, the thread groups that may be executed each time are different) * Set Dependencies
[OperationB addDependency: operationA]; // operation B depends on operation
3> Custom Operation (basic procedures)
4> how to solve the problem of repeated download of an image (a url)
Master haomeng is devoted to his contribution and respects the author's Labor achievements. Do not repost them.
If the article is helpful to you, you are welcome to donate to the author, support haomeng master, the amount of donation is free, focusing on your mind ^_^
I want to donate: Click to donate
Cocos2d-X source code download: point I send
Game official download: http://dwz.cn/RwTjl
Game video preview: http://dwz.cn/RzHHd
Game Development blog: http://dwz.cn/RzJzI
Game source code transfer: Http://dwz.cn/Nret1
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.