The execution principle of multithreading
The CPU can only process one thread at a time, and only one thread is executing. Multi-threaded “simultaneous” execution is the rapid switching of the CPU between multiple threads. CPU execution is fast, causing the CPU to schedule threads short enough to create the illusion of simultaneous execution. Threads can improve the efficiency of program execution, but it is not the more threads the better, too many threads will lead to frequent switching between CPUS, consuming a lot of resources.
IOS multithreading technical scheme
The GCD is introduced
Grand Central Dispatch is a pure C language with many powerful function libraries
- GCD is apple’s solution to multi-core parallel computing
- The GCD automatically uses more CPU cores
- The GCD automatically manages the thread lifecycle
- Programmers just tell the CCP what code they want to execute without writing any thread management code
Introduction of GCD
1. Two cores of GCD 1. Tasks: operations to be performed 2. Queues: Used to store tasks 2. Create a task. 3. Add the task to the queue
- The GCD will automatically remove the tasks from the queue and put them into the corresponding thread for execution
- Task fetching follows the FIFO principle of queue: first in, first out, last in, last out
The way the COMMUNIST party performs its tasks
1. Perform tasks synchronously
- Tasks are executed in the main thread without the ability to start a new thread
2. Perform tasks asynchronously
- Perform tasks on a new thread and have the ability to start a new thread
Type of GCD queue
1. Concurrent queues allow multiple tasks to execute simultaneously. This function only works with asynchronous functions
GCD various queue execution effects
If you have a task on the main thread, you have to wait for the main thread to finish before you turn to the main queue. The main queue must be executed on the main thread. The main queue is a global serial queue
Code demo
// 1 Serial queue executes synchronously. No new threads are opened and tasks are executed sequentially
- (void) demo1{
NSLog(@ "start");
dispatch_queue_t queue = dispatch_queue_create("GT", DISPATCH_QUEUE_SERIAL);
for (int i = 0 ; i < 10; i++) {
dispatch_sync(queue, ^{
NSLog(@"hello %d, %@",i, [NSThread currentThread]);
});
}
NSLog(@ "end");
}
//2 Serial queue asynchronous execution starts a thread. Tasks are still performed sequentially
- (void) demo2{
NSLog(@ "start");
dispatch_queue_t queue = dispatch_queue_create("GT", DISPATCH_QUEUE_SERIAL);
for (int i = 0 ; i < 10; i++) {
dispatch_async(queue, ^{
NSLog(@"hello %d, %@",i, [NSThread currentThread]);
});
}
NSLog(@ "end");
}
//3 Parallel queue, synchronous execution (same as serial queue, synchronous execution type) no thread, sequential execution
- (void) demo3{
NSLog(@ "start");
dispatch_queue_t queue = dispatch_queue_create("GT", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0 ; i < 10; i++) {
dispatch_sync(queue, ^{
NSLog(@"hello %d, %@",i, [NSThread currentThread]);
});
}
NSLog(@ "end");
}
//4 Parallel queues execute asynchronously. Open multiple threads. Unordered execution, threads shared
- (void) demo4{
NSLog(@ "start");
dispatch_queue_t queue = dispatch_queue_create("GT", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0 ; i < 10; i++) {
dispatch_async(queue, ^{
NSLog(@"hello %d, %@",i, [NSThread currentThread]);
});
}
NSLog(@ "end");
}
//5 Primary queue asynchronous execution is executed sequentially on the main thread
// The code in the main thread is executed before the tasks in the main queue are executed
- (void) demo5{
/ / the home team
NSLog(@ "start");
for (int i = 0; i < 10; i++) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"hello %d %@",i,[NSThread currentThread]);
});
}
NSLog(@ "end");
}
//6 Main queue execution synchronously -- deadlocks occur only when executed on the main thread.
// Synchronous execution: wait for the first task to complete before continuing.
- (void) demo6{
NSLog(@ "start");
for (int i = 0; i < 10; i++) {
/ / a deadlock
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"hello %d %@",i,[NSThread currentThread]);
});
}
NSLog(@ "end");
}
//7 Resolve deadlock issues
- (void) demo7{
NSLog(@ "start");
dispatch_async(dispatch_get_global_queue(0.0), ^ {for (int i = 0; i < 10; i++) {
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"hello %d %@",i,[NSThreadcurrentThread]); }); }});NSLog(@ "end");
}
end
Copy the code