Multithreading in iOS development-GCD and multithreading in ios development
This article is based on wending's blog study. If anything is inappropriate, I hope to point it out. Http://www.cnblogs.com/wendingding/p/3807716.html
I. Tasks and queues
There are two core concepts in GCD
(1) task: What operations are performed?
(2) queue: used to store tasks
There are two steps to use GCD.
(1) custom tasks
(2) determine what you want to do
After a task is added to the queue, GCD automatically extracts the task from the queue and puts it in the corresponding thread for execution.
Tip: the removal of tasks follows the FIFO principle of the queue: first-in-first-out, and later-in and later-out.
Ii. Execute the task
1. There are two functions in GCD used to execute the task.
Note: Submit the parameter (task) on the right to the parameter (Queue) on the left for execution.
(1) execute the task dispatch_sync (dispatch_queue_t queue, dispatch_block_t block) in synchronous mode );
Parameter description:
Queue: queue
Block: Task
(2) execute the task dispatch_async (dispatch_queue_t queue, dispatch_block_t block) in asynchronous mode );
2. Differences between synchronization and Asynchronization
Synchronization: executed in the current thread
Asynchronous: executed in another thread
3. Queue
1. Queue type
GCD queues can be divided into two types:
(1) Concurrent Dispatch Queue)
Concurrent execution of multiple tasks (automatically enabling multiple threads to execute tasks simultaneously) is only effective under the asynchronous (dispatch_async) function.
(2) Serial Queue (Serial Dispatch Queue)
Let the task be executed one by one (after a task is executed, execute the next task)
2. Additional instructions
Four terms are confusing: synchronous, asynchronous, concurrent, and serial
Synchronization and Asynchronization determine whether to enable new threads
Synchronization: The task is executed in the current thread and cannot start a new thread.
Asynchronous: execute tasks in new threads and enable new threads.
Concurrency and serial determine the task execution mode.
Concurrency: Concurrent (concurrent) Execution of multiple tasks
Serial: After a task is executed, execute the next task.
3. Serial queue
There are two methods to obtain serial numbers in GCD.
(1) Use the dispatch_queue_create function to create a serial queue
Dispatch_queue_t dispatch_queue_create (const char * label, dispatch_queue_attr_t attr); // queue name, queue attribute, which can be NULL
Example:
Dispatch_queue_t queue = dispatch_queue_create ("wendingding", NULL); // create
Dispatch_release (queue); // non-ARC queue needs to be released manually
(2) Use the main queue column (queue associated with the main thread)
The main queue is a special serial queue that comes with GCD. All tasks in the main queue are executed in the main thread.
Use dispatch_get_main_queue () to obtain the main queue Column
Example:
Dispatch_queue_t queue = dispatch_get_main_queue ();
4. Concurrent queue
By default, GCD provides a global concurrent queue for the entire application.
Use the dispatch_get_global_queue function to obtain the global concurrent queue.
Dispatch_queue_t dispatch_get_global_queue (dispatch_queue_priority_t priority, unsigned long flags); // this parameter is useless for the moment. Use 0.
Example:
This parameter is left for future use. If it is unavailable for the moment, 0 is passed.
The first parameter is the priority. The default parameter is selected here. Obtains a global concurrency queue with the default priority.
Dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // obtain the global concurrency queue
Common creation methods:
// Use the synchronization function to add tasks to the serial queue without opening a new thread dispatch_sync (dispatch_queue_create ("syncqqq", NULL), ^ {NSLog (@ "currentThread = % @", [NSThread currentThread]) ;}); // currentThread = <NSThread: 0x17006bb00> {number = 1, name = main} // use the synchronization function to add tasks to the concurrent queue, dispatch_sync (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {NSLog (@ "currentThread = % @", [NSThread currentThread]);}); // currentThread = <NSThread: 0x17006bb00> {number = 1, name = main} // Add tasks to the serial queue using asynchronous functions to open up new threads (only one thread is opened, task serial execution) dispatch_queue_t queue = dispatch_queue_create ("www", NULL); dispatch_async (queue, ^ {for (int I = 0; I <10000; I ++) {NSLog (@ "currentThread1 = % @", [NSThread currentThread]) ;}}); dispatch_async (queue, ^ {NSLog (@ "currentThread2 = % @", [NSThread currentThread]) ;}); // currentThread1 = <NSThread: 0x17007c500> {number = 4, name = (null)} // currentThread2 = <NSThread: 0x17007c500> {number = 4, name = (null)} // use an asynchronous function to add a task to the concurrent queue and open up a new thread (How many threads are opened and the task is executed concurrently) dispatch_queue_t aqueue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async (aqueue, ^ {for (int I = 0; I <10000; I ++) {NSLog (@ "currentThread3 = % @", [NSThread currentThread]) ;}}); dispatch_async (aqueue, ^ {NSLog (@ "currentThread4 =% @", [NSThread currentThread]) ;});
Note: Synchronous functions do not have the ability to enable threads. threads are not enabled in any queue. asynchronous functions can enable threads, the number of threads enabled is determined by the queue (only one new thread is enabled for the serial queue and multiple threads are enabled for the concurrent Queue ).
Synchronous Functions
(1) Concurrent queue: no thread is enabled
(2) Serial queue: no thread is enabled
Asynchronous Functions
(1) Concurrent queue: Enabling N threads
(2) Serial queue: enable one thread
Common usage:
1. Return from sub-thread to main thread
Dispatch_async (dispatch_get_global_queue (queue, 0), ^ {// perform some time-consuming operations dispatch_async (dispatch_get_main_queue (), ^ {// return to the main thread for operations });});
Note:
Dispatch_sync (dispatch_get_main_queue (), ^ {
});
This writing method is incorrect and causes program crash.
2. Delayed call
(1)
[Self defined mselector: @ selector (run) withObject: nil afterDelay: 5.0];
Note:
[Self defined mselector: @ selector (run) withObject: nil afterDelay: 5.0]; put it in a child thread, for example:
Dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
[Self defined mselector: @ selector (run) withObject: nil afterDelay: 5.0];
});
At this time, the program will not run the run method.
Correct syntax:
Dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
[Self defined mselector: @ selector (run) withObject: nil afterDelay: 5.0];
[[Nsunloop currentRunLoop] run];
});
(2)
Dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (5.0 * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {
// The operation will be executed in five seconds. You can select which thread to execute. That is, dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) can be used to replace dispatch_get_main_queue ()
});