iOS - Multithreaded GCD

1. What is GCD

1. GCD is a set of libraries defined by Apple to solve multi-threading, and GCD can automatically manage the life cycle of threads, which is similar to ARC, and does not require us to manage manually

2. GCD is written in pure C language Written, so I use functions in

GCD , not object-oriented

methods 1) Determine the task to be executed 2) Add the task to the queue, GCD will automatically take out the task in the queue and put it in the corresponding thread to execute 5. Synchronous and asynchronous 1) Synchronous: Execute tasks in the same thread, No new thread will be created // Synchronous function // Parameter 1: Queue // Parameter 2: The code block of the task dispatch_sync(dispatch_queue_t queue, DISPATCH_NOESCAPE dispatch_block_t block); copy code 2) Asynchronous: create a new thread, and in the new Execute the task in the thread // asynchronous function // parameter 1: queue // parameter 2: the code block of the task dispatch_async(dispatch_queue_t queue, dispatch_block_t block) ;























6. Queues Queues can be divided into two types
: 1) Asynchronous queues: Queues executed in parallel, each task in the queue can be executed concurrently (synchronously)


2) Serial queues: Queues executed serially, each task in the queue Each task needs to be executed serially, that is, one by one to


obtain the queue
// create a serial queue
// parameter 1: queue name, C-style string
// parameter 2: the attribute of the queue, generally use NULL to
dispatch_queue_t dispatch_queue_create(const char *_Nullable label, dispatch_queue_attr_t _Nullable attr);
copy code
dispatch_queue_t is the type of the queue
// Get the main queue, the main queue is a serial queue, and corresponds to the main thread, the tasks in the main queue will be executed by the main thread
dispatch_queue_t dispatch_get_main_queue(void );
Copy code
// Global concurrent queue, can be used by the entire application, no need to manually create
// Parameter 1: Queue priority (there are 4)
// #define DISPATCH_QUEUE_PRIORITY_HIGH 2 High priority
// #define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 default
// #define DISPATCH_QUEUE_PRIORITY_LOW (-2) low priority
// Parameter 2: The attributes of the queue, you can wear 0
dispatch_queue_t dispatch_get_global_queue(long identifier, unsigned long flags);
copy code
2. GCD basic application
1. Asynchronous/synchronous functions and serial/parallel queues

1) Use asynchronous functions to send concurrent queues Add tasks in
// 1. Print the main thread
NSLog(@"Main thread--- %@", [NSThread currentThread]);

// 2. Get the global concurrent queue and set the priority to the default
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT , 0);
   
// 3. Add tasks to the parallel queue, you can execute tasks
// Add tasks using asynchronous functions, you can open new threads
dispatch_async(queue, ^{
   
    NSLog(@"Task 1 --- %@ ", [NSThread currentThread]);
   
});
   
dispatch_async(queue, ^{
       
    NSLog(@"Task 2 --- %@", [NSThread currentThread]);
       
});
   
   
dispatch_async(queue, ^{
       
    NSLog(@"Task 3 --- %@", [NSThread currentThread]);
       
});
Copy code
Running result
333.pngSummary
: It can be seen that in addition to the main thread, three sub-threads are also created, and The three sub-threads are executed concurrently

2) Use asynchronous functions to add tasks to the serial queue
// 1. Create a serial queue
// Parameter 1: The name of the serial queue, which is a C-style string
// Parameter 2: Serial The attributes of the queue, generally speaking, serial queues do not need any attributes, you can pass NULL
dispatch_queue_t queue = dispatch_queue_create("Chuanxin", NULL);
   
NSLog(@"Main thread--- %@", [NSThread currentThread]);
   
// 2. Use an asynchronous function to add tasks to the serial queue
dispatch_async(queue, ^{
       
    NSLog(@"Task 1 --- %@", [NSThread currentThread]);
       
});
   
dispatch_async(queue, ^{
       
    NSLog( @"Task 2 --- %@", [NSThread currentThread]);
       
});
   
dispatch_async(queue, ^{
       
    NSLog(@"Task 3 --- %@", [NSThread currentThread]);
       
});
Copy code
Run result
4444.pngSummary
: When adding a task to the serial queue using an asynchronous function, a new thread will be opened, but Only one will be opened; because the tasks in the serial queue need to be executed one by one, they do not have to be executed at the same time, so only a new new thread will be created

3) Use the synchronization function to add tasks to the parallel queue
NSLog(@"Main thread---% @", [NSThread currentThread]);

// 1. Get the global parallel queue and set the priority to the default
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
   
// 2. Use the synchronization function to add tasks to the parallel queue
dispatch_sync( queue, ^{
       
    NSLog(@"Task 1 --- %@", [NSThread currentThread]);
       
});
   
dispatch_sync(queue, ^{
   
    NSLog(@"Task 2 --- %@", [NSThread currentThread]) ;
   
});
   
dispatch_sync(queue, ^{
       
    NSLog(@"Task 3 --- %@", [NSThread currentThread]);
       
});
Copy code
Run result
555.png
555.png (44.47 KB, Downloads: 0)
Download attachment
Uploaded 5 seconds ago
Summary : Because The synchronization function is used, so no new thread is created, so it is executed in the main thread; at this time, the concurrent queue loses its function, because no new thread is created, so what about concurrency?
Original link: http: https://bbs.jointforce.com/topic/26514

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326347427&siteId=291194637
gcd