The first thing to know is the difference between threads and processes. Every application running on a system is a thread. and the tasks to be performed in the process are implemented on-line, so that the thread is the smallest execution unit of the process.
The process must have a minimum of one thread. Multithreading, as the name implies, is more than one thread.
So in the iOS development of the use of multithreading are what, what is the difference, below we come to the detailed spit trough a wave.
When it comes to multithreading, we have to mention POSIX threads, abbreviated pthreads. He is a complete set of APIs for managing threads in Unix-like operating systems. The specific pthreads himself asked the sexy charming degree empress! There are 3 types of thread management that we encounter in iOS development, namely:nsthread,nsoperation(nsoperationqueue) and GCD
First,nsthread
Nsthread the most we use in development is using "Nsthread CurrentThread" to get the current thread. He is relatively easy to perform some simple single task, but when to perform multiple tasks, and there is a certain association between the tasks, there are execution order, this time nsthread is too weak.
Second,GCD
The most we use in the development process is the GCD. He is a C-based set of multithreaded development APIs. It's easy to use, we don't have to manage the thread's life cycle, we just need to focus on the tasks we perform and the correlation between the tasks, and GCD helps us manage the thread life cycle. GCD summed up is the queue + execution mode. This means that we need to put the executed tasks in the queue and then execute them in a way.
As for the queue, GCD provides us with two kinds of queues: Serial queue and parallel queue, and here we need to mention two special queues that already exist in the program, they are the home column (a serial queue that the program creates for us), and the global queue (a parallel queue that the program created for us)
Serial queue: A serial queue in which the tasks on this queue must be executed one after the other and must wait for one of the previous tasks to complete before you can proceed with the following tasks. And the serial queue does not have the ability to open new threads.
Parallel queues: Parallel queues, which means that tasks on this queue do not have to wait for the previous task to complete to perform other tasks, he is waiting for the task to execute concurrently with multiple tasks. And the parallel queue has the ability to open new threads, but he does not necessarily have to open a new thread to perform the task, mainly watching the CPU scheduling.
Synchronous execution: You must wait for the Blcok in the Dispatch_sync () function to complete before returning the function, while synchronous execution does not open a new thread.
Asynchronous execution: the Dispatch_async () function returns immediately without waiting for the block inside to execute, which can be understood to perform the task in block in the next execution cycle. Asynchronous execution can open a new thread, but not necessarily open a new thread.
Here are a few combinations of queues and how to perform them.
1. Serial Queue + synchronous execution (the serial queue here cannot be the home column because it will deadlock)
We can see that the tasks are executed sequentially.
2. Serial Queue + asynchronous execution
We can see that the Dispatch_async () function does not work until the block executes, and then executes the following task. The task in block was performed at the next execution cycle.
3. Parallel Queue + synchronous execution
The tasks are still performed sequentially. Synchronous execution must wait for the task in the block to complete before returning the dispatch function before performing the following tasks
4. Parallel Queue + asynchronous execution
Tasks are not executed sequentially.
Summary of multi-threading in iOS