Five queue usage and asynchronous and synchronous operations of ios gcd, iosgcd
Advantages of GCD: 1. GCD is easier to use than thread, and the Block-based feature enables it to transmit context between different code blocks.
First: dispatch_queue
(1) Serial Dispatch Queue --- Wait Until processing of the currently executed task ends (Serial)
(2) Concurrent Dispatch Queue --- do not wait until the processing of the currently executed task ends (parallel and Concurrent)
Dispatch_queue_t queue1 = dispatch_queue_create ("com. wxhl. gcd. Queue1", NULL );
Dispatch_queue_t queue2 = dispatch_queue_create ("com. wxhl. gcd. Queue2", DISPATCH_QUEUE_CONCURRENT); // parallel queue
// (2) Create the task to be executed and add it to the queue for execution
Dispatch_async (queue2, ^ {
For (int I = 0; I <50; I ++ ){
NSLog (@ "GCD: % d", I );
}
});
Dispatch_async (queue2, ^ {
For (int I = 0; I <50; I ++ ){
NSLog (@ "GCD2 ------: % d", I );
}
});
2nd types: dispatch_after
// After dispatch_after, add the task to the queue.
Dispatch_time_t time = dispatch_time (DISPATCH_TIME_NOW, 3ull * NSEC_PER_SEC );
// NSEC_PER_SEC seconds
// NSEC_PER_MSEC millisecond
// NSEC_PER_USEC microseconds
Dispatch_after (time, dispatch_get_main_queue (), ^ {
NSLog (@ "task 1 ");
});
// Method 2
Dispatch_after_f (dispatch_time (DISPATCH_TIME_NOW, 6ull * NSEC_PER_SEC), dispatch_get_main_queue (), NULL, fun );
// Use it by yourself
Dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (5ull * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {
NSLog (@ "task 3 ");
});
}
Void fun (){
NSLog (@ "task 2 ");
}
3rd types: dispatch_group
// Execute tasks in parallel queues and add multiple serial queues to parallel queues
// Disptch_group
Dispatch_group_t group = dispatch_group_create ();
Dispatch_queue_t queue = dispatch_get_global_queue (0, 0 );
Dispatch_group_async (group, queue, ^ {
NSLog (@ "task 01 ");
});
Dispatch_group_async (group, queue, ^ {
NSLog (@ "task 02 ");
});
Dispatch_group_async (group, queue, ^ {
Sleep (6); // wait 6 seconds to execute the task 3 Objective: To test the method of dispatch_group_wait
NSLog (@ "task 03 ");
});
Dispatch_group_async (group, queue, ^ {
Sleep (2 );
NSLog (@ "task 04 ");
});
// Monitoring function
// Stops tasks in the monitoring queue and executes tasks in the block.
Dispatch_group_policy (group, queue, ^ {
NSLog (@ "done ");
});
// Wait time
Dispatch_time_t time = dispatch_time (DISPATCH_TIME_NOW, 5ull * NSEC_PER_SEC );
// After the time specified by dispatch_group_wait, check whether the queue execution is complete.
// If 0 is returned after execution
// If the execution is not completed, a non-0 value is returned.
Long result = dispatch_group_wait (group, time );
If (result = 0 ){
NSLog (@ "finish ");
} Else {
NSLog (@ "not finish ");
}
}
4th types: dispatch_once (GCD implementation Singleton)
// Create a singleton Method
+ (Exactly once *) shanreInstence {
Static dispatch_once_t onceToken;
Dispatch_once (& onceToken, ^ {
Instance = [[ShareOncealloc] init];
NSLog (@ "only 1 execution ");
});
Returninstance;
}
// Alloc will automatically call the allocWithZone Method
// Zone space automatically allocates memory space to create objects
+ (Id) allocWithZone :( struct_NSZone *) zone {
If (instance = nil ){
Instance = [superallocWithZone: zone];
}
Returninstance;
}
// Call the singleton method [exactly once attached instance];
5th types: dispatch_semaphore
// Use
Dispatch_queue_t queue = dispatch_get_global_queue (0, 0 );
Dispatch_semaphore_t dsema = dispatch_semaphore_create (1 );
NSMutableArray * array = [NSMutableArrayarray];
For (int I = 0; I <1000; I ++ ){
Dispatch_async (queue, ^ {
Dispatch_semaphore_wait (dsema, DISPATCH_TIME_FOREVER );
[ArrayaddObject: [NSNumbernumberWithInt: I];
Dispatch_semaphore_signal (dsema );
});
}
NSLog (@ "% @", array );
6th types: Asynchronous Operation: dispatch_async synchronous operation: dispatch_sync
// Dispatch sync
//
// Async: asynchronous asynchronously appends the task to the queue.
Dispatch_async (dispatch_get_global_queue (0, 0), ^ {
NSLog (@ "async ");
});
// Sync: synchronous appends the synchronization tasks to the queue (wait until the tasks in the queue are completed and then append the tasks to the queue)
// Synchronous append, not synchronous task execution. In the serial queue, the task is executed synchronously.
Dispatch_sync (dispatch_get_global_queue (0, 0), ^ {
NSLog (@ "sync ");
});
// Dispatch_sync: deadlock is prone
// Example 1:
Dispatch_sync (dispatch_get_main_queue (), ^ {
NSLog (@ "hello ");
});
NSLog (@ "main thread ");
// Execute the specified block in the main queue and wait until the execution ends.
// The above code is already executed in the main queue column, and the append block cannot be executed.
// Example 2:
// Serial queue
Dispatch_queue_t queue = dispatch_queue_create ("com. wxhl. GCD. queue", NULL );
Dispatch_async (queue, ^ {
Dispatch_sync (queue, ^ {
NSLog (@ "Serial queue ");
});
});