Shimen the main contribution, respect the work of the author, please do not reprint.
If the article is useful to you, you are welcome to donate it to the author. Support Shimen Master, donate amount arbitrarily, heavy in mind ^_^
I want to donate: Click Donate
Cocos2d-x source code Download: Dot I teleport
Games 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. Processwhat is a process
A process is an application that is running in the systemeach process is independent of one another. Each process runs within its dedicated and protected memory spaceFor example, open QQ, Xcode at the same time, the system will start 2 processes respectively"Activity Monitor" enables you to view the processes that are open in your Mac system B. Threadsmain thread, sub-thread occupy memory each is 1M and 512K1. What is a thread
1 processes to run a task. Must have a thread (at least 1 threads per 1 processes)a thread is the basic running unit of a process. All tasks of a process (program) are run in a threadFor example, use a cool dog to play music and use Thunder to download movies. are required to run in the thread 2. Thread's serialrunning a task in 1 threads is serial
Assume that you want to run multiple tasks in 1 threads. So you can just run these tasks one at A--in order.That is , 1 threads can only run 1 tasks at a timeFor example, download 3 files in 1 threads (each of which is file a, file B, file c) 3. MultithreadingWhat is multithreading
More than one thread can be opened in 1 processes. Each thread can run different tasks in parallel (at the same time)
Process shop, thread shop workermultithreading technology can improve the operation efficiency of the programFor example, open 3 threads at the same time to download 3 files (respectively, file A, file B, file c) 4. Multithreading principlethe principle of multi-threading
At the same time, the CPU can only handle 1 threads. Only 1 threads are working (running)
Multi-threaded concurrency (same time) running, in fact, the CPU is high-speed scheduling between multiple threads (switching)
Assuming that the CPU schedules the thread fast enough, it creates the illusion of multi-threaded concurrency running
Think: What happens when you assume a lot of threads?
The CPU is dispatched between N multithreading. CPU is exhausted and consumes a lot of CPU resources
The frequency at which each thread is scheduled to run is reduced (the running efficiency of the thread is reduced) 5. Advantages and disadvantages of multithreadingAdvantages of multithreading
Can improve the operation efficiency of the program properly
Can appropriately improve resource utilization (CPU, memory utilization)
Disadvantages of multithreading
Open threads need to occupy a certain amount of memory (by default, the main thread takes up 1M and the child threads occupy 512KB), assuming that a large number of threads are turned on. Consumes a lot of memory space and reduces program performance
The more threads, the greater the overhead of the CPU on the dispatch thread
Programming is more complex: for example, communication between threads, multi-threaded data sharing 6. Main ThreadWhat is the main thread
After an iOS program runs. By default, 1 threads are turned on. Called "Main thread" or "UI thread"
Main functions of the primary thread
Display \ Refresh UI interface
Handling UI events (such as click events, scrolling events, drag events, etc.)
Note on the use of the main thread
Do not put more time-consuming operations into the main threadThe time-consuming operation will get stuck in the main thread. Seriously affect the smoothness of the UI, give users a "card" bad experience 7.iOS Multithreading Implementation Technology
- Pthread
- Nsthread
- GCD
- Nsoperation
C. What needs to be masteredMultithreading
1.NSThread
Several ways to 1> the thread
* First created, then started
Nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (run) Object:nil];
[Thread start];
* Direct Start
[Nsthread detachnewthreadselector: @selector (Run) totarget:self Withobject:nil];
[Self Performselectorinbackground: @selector (Run) Withobject:nil];
2> other ways to use
Nsthread *current = [Nsthread CurrentThread];
+ (Nsthread *) Mainthread; Get the main thread
3> Inter-thread communication
Performselectoronmainthread .....
2.GCD (Focus)
1> Types of queues
* Concurrent Queue
Get a global concurrent queue: Dispatch_get_global_queue
* Serial Queue
A. Create yourself
Dispatch_queue_create
B. Home row
Dispatch_get_main_queue
2> method types for running tasks
* Synchronization (Sync) operation
* Asynchronous (Async) Run
3> Understanding how queues and methods work together
4> Inter-thread communication
Dispatch_async (
Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), ^{
Run time-consuming asynchronous operations ...
Dispatch_async (Dispatch_get_main_queue (), ^{
Back to the main thread. To run a UI refresh operation
});
});
5> other ways to use
Dispatch_once
Dispatch_after
Dispatch_group_async\dispatch_group_notify
3.NSOperation
1> Basic Use
Nsinvocationoperation
Nsblockoperation
2> Nsoperationqueue (Key)
* Maximum number of concurrent settings-(void) Setmaxconcurrentoperationcount: (Nsinteger) CNT; Note: This method can only limit the number of threads running at the same time. You cannot restrict a specific thread (you can run a different set of threads each time) * Set Dependencies
[Operationb Adddependency:operationa]; Action B depends on action a
3> Define your own operation (understand the basic process)
4> How to solve a picture (a URL) repeated download problem
Shimen the main contribution of the Lord, respect the author's labor results. Please do not reprint.
Suppose the article is helpful to you. Welcome to donate to the author, support Shimen master, donate amount arbitrarily, heavy in mind ^_^
I want to donate: Click Donate
Cocos2d-x source code Download: Dot I teleport
Games 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
iOS multi-Threading and Web development multi-Threading Overview