Objetive-C learning _ GCD learning notes, objetive-c_gcd
GCD: Apple's solution for multi-core parallel operations
GCD automatically manages the thread lifecycle (creating threads, scheduling tasks, and destroying threads)
Queue: used to store tasks (Serial queue and parallel Queue)
Task: What operations are performed (synchronous and asynchronous)
Concurrency: multiple tasks are executed simultaneously.
Serial: After a task is executed, execute the next task.
|
Global concurrent Queue (multiple) |
Manually create a serial Queue (1) |
Main Queue (0) |
Sync) 0 Required |
No new thread Enabled Serial task execution |
No new thread Enabled Serial task execution |
No new thread Enabled Serial task execution |
Asynchronous (async) Multiple |
New threads Enabled Concurrent task execution |
New threads Enabled Serial task execution |
No new thread Enabled Serial task execution |
My memory method (just the memory method): regards the queue as capable of opening new threads, but they have limited capabilities and cannot be opened infinitely. They regard the task as the number of threads required. Obtain the number of first-open threads (such as the number in the table) based on the requirements and queue capabilities ).
Method:
Execute tasks in synchronous mode: dispatch_sync (dispatch_queue_t queue, dispatch_block_t block );
Asynchronous task execution: dispatch_async (dispatch_queue_t queue, dispatch_block_t block );
Obtain the global concurrency queue: dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 );
Create a serial queue: dispatch_queue_create (const char * label, dispatch_queue_attr_t attr); // queue name and queue attribute (NULL is generally used)
Obtain the main queue column: dispatch_get_main_queue ();
Latency functions:
(1) [self defined mselector :( SEL) withObject :( id) afterDelay :( NSTimeInterval)]; // object method, only one method can be written in the current thread
(2) dispatch_after (dispatch_time (dispatch_time_t when, int64_t delta), dispatch_queue_t queue, ^ (void) block)
Parameter description:
// When can be set to 0 or use DISPATCH_TIME_NOW to indicate that
// Delta is generally passed in (int64_t) (X * NSEC_PER_SEC). The number of seconds after which X indicates
// The NSEC_PER_SEC output is 1000000000.
// Dispatch_time_t is actually unsigned long
// Int64_t is actually long
Singleton mode:
+ (Id) ShareManager
{
Static MyManager * staticInstance = nil;
Static dispatch_once_t onceToken;
Dispatch_once (& onceToken, ^ {
StaticInstance = [[self alloc] init];
});
Return staticInstance;
}
Queue group usage steps: (perform operations after multiple time-consuming operations are completed)
1. Create a queue group: dispatch_group_t group = dispatch_group_create ();
2. Create a group task and add it to the group: dispatch_group_async (dispatch_group_t group, dispatch_queue_t queue, ^ (void) block );
3. After all the tasks in the queue group are completed, execute other operations: dispatch_group_notify (dispatch_group_t group, dispatch_queue_t queue, ^ (void) block );
Notes and knowledge points:
(1) If non-ARC is used, the queue should be released when it is created. All function names contain create/copy/new/retain, you do not need to use this data for release. However, the data type of CF (core Foudation) must be release in the ARC environment.
(2) The synchronization function cannot be placed in the main thread. Adding a task to the main queue in the main thread will get stuck ). Because the serial queue executes the next task only after a task is executed.
(3) GCD provides a global concurrency queue for the entire application. You do not need to create it manually. You need to create a serial queue yourself.
(4) FIFO principle of task removal queue: first-in-first-out and later-out