“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Queue creation

Create custom concurrent and serial queues

// Parallel queue
dispatch_queue_t queue = dispatch_queue_create("com.gcd.queue", DISPATCH_QUEUE_CONCURRENT);

// Serial queue
dispatch_queue_t queue = dispatch_queue_create("com.gcd.queue", NULL);
Copy the code

Parameter 2: Queue type, set to NULL. Serial queue (DISPATCH_QUEUE_SERIAL) is created by default. Create parallel queues set the parameter to DISPATCH_QUEUE_CONCURRENT

Get the global concurrent queue

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
Copy the code

Parameter 1: Priority DISPATCH_QUEUE_PRIORITY_HIGH// High priority DISPATCH_QUEUE_PRIORITY_DEFAULT// Default priority DISPATCH_QUEUE_PRIORITY_LOW// Low priority DISPATCH_QUEUE_PRIORITY_BACKGROUND (DISPATCH_QUEUE_PRIORITY_BACKGROUND)

Get global main queue (special serial queue)

  • Tasks placed in the main queue are executed in the main thread
  • The main queue is essentially a normal serial queue, only because by default, the current code is placed in the main queue, and then the code in the main queue is executed in the main thread
dispatch_queue_t queue = dispatch_get_main_queue();
Copy the code

The differences between different combinations of tasks and queues

Concurrent queue + asynchronous function

  • Multiple threads can be opened and tasks executed simultaneously (asynchronous functions have the ability to open new threads)

Example:

dispatch_queue_t concurrentQueue = dispatch_queue_create("com.gcd.concurrentQueue", DISPATCH_QUEUE_CONCURRENT);

NSLog(@"Current thread --0--%@",[NSThread currentThread]);

// Asynchronous functions
dispatch_async(concurrentQueue, ^{
    for (NSInteger i = 0; i < 5; i++) {
        NSLog(@"Asynchronous function --1--%@",[NSThread currentThread]); }});// Asynchronous functions
dispatch_async(concurrentQueue, ^{
    for (NSInteger i = 0; i < 5; i++) {
        NSLog(@"Asynchronous function --2--%@",[NSThread currentThread]); }});Copy the code

The log:

Concurrent queue + synchronization function

  • A task is executed in the current thread, and no new thread is opened. After one task is executed, the next task is executed
  • The reason for sequential execution: While concurrent queues can start multiple threads and perform multiple tasks at the same time, synchronization functions do not have the ability to start new threads, so there is no concurrency. Therefore, tasks can only be executed sequentially, not simultaneously.

Example:

dispatch_queue_t concurrentQueue = dispatch_queue_create("com.gcd.concurrentQueue", DISPATCH_QUEUE_CONCURRENT);

NSLog(@"Current thread --0--%@",[NSThread currentThread]);

// synchronization function
dispatch_sync(concurrentQueue, ^{
    for (NSInteger i = 0; i < 5; i++) {
        NSLog(@"Synchronization function --1--%@",[NSThread currentThread]); }});// synchronization function
dispatch_sync(concurrentQueue, ^{
    for (NSInteger i = 0; i < 5; i++) {
        NSLog(@"Synchronization function --2--%@",[NSThread currentThread]); }});Copy the code

The log:

Serial queue + asynchronous function

  • A new thread will be opened, the task is serial, after the execution of one before the next (asynchronous function can start a new thread).

Example:

dispatch_queue_t serialQueue = dispatch_queue_create("com.gcd.serialQueue", DISPATCH_QUEUE_SERIAL);

NSLog(@"Current thread --0--%@",[NSThread currentThread]);

// Asynchronous functions
dispatch_async(serialQueue, ^{
    for (NSInteger i = 0; i < 5; i++) {
        NSLog(@"Asynchronous function --1--%@",[NSThread currentThread]); }});// Asynchronous functions
dispatch_async(serialQueue, ^{
    for (NSInteger i = 0; i < 5; i++) {
        NSLog(@"Asynchronous function --2--%@",[NSThread currentThread]); }});Copy the code

The log:

Serial queue + synchronization function

  • All tasks are executed in the current thread (main thread), no new thread is started (synchronization function does not have the ability to start a new thread)

Example:

dispatch_queue_t serialQueue = dispatch_queue_create("com.gcd.serialQueue", DISPATCH_QUEUE_SERIAL);

NSLog(@"Current thread --0--%@",[NSThread currentThread]);

// synchronization function
dispatch_sync(serialQueue, ^{
    for (NSInteger i = 0; i < 5; i++) {
        NSLog(@"Synchronization function --1--%@",[NSThread currentThread]); }});// synchronization function
dispatch_sync(serialQueue, ^{
    for (NSInteger i = 0; i < 5; i++) {
        NSLog(@"Synchronization function --2--%@",[NSThread currentThread]); }});Copy the code

log:

Main queue + asynchronous function

  • All tasks are executed in the current thread (main thread) and no new thread is started (asynchronous execution has the ability to start threads, but because it is the main queue, all tasks are in the main thread).

Example:

dispatch_queue_t mainQueue = dispatch_get_main_queue();

NSLog(@"Current thread --0--%@",[NSThread currentThread]);

// Asynchronous functions
dispatch_async(mainQueue, ^{
    for (NSInteger i = 0; i < 5; i++) {
        NSLog(@"Asynchronous function --1--%@",[NSThread currentThread]); }});// Asynchronous functions
dispatch_async(mainQueue, ^{
    for (NSInteger i = 0; i < 5; i++) {
        NSLog(@"Asynchronous function --2--%@",[NSThread currentThread]); }});Copy the code

The log:

Main queue + synchronization function

The synchronization task added to the main queue and the main thread itself wait on each other, blocking the main queue and eventually causing a deadlock on the main thread

Example:

dispatch_queue_t mainQueue = dispatch_get_main_queue();

NSLog(@"Current thread --0--%@",[NSThread currentThread]);

// synchronization function
dispatch_sync(mainQueue, ^{
    for (NSInteger i = 0; i < 5; i++) {
        NSLog(@"Synchronization function --1--%@",[NSThread currentThread]); }});// synchronization function
dispatch_sync(mainQueue, ^{
    for (NSInteger i = 0; i < 5; i++) {
        NSLog(@"Synchronization function --2--%@",[NSThread currentThread]); }});Copy the code

The log:

The execution effect of various queues

type Concurrent queue Serial queues The home side column
Sync No new thread will be opened

Serial execution of tasks
No new thread will be opened

Serial execution of tasks
The deadlock is stuck and will not be executed
Asynchronous (async) A new thread will be opened

Parallel task execution
A new thread will be opened

Serial execution of tasks
No new thread will be opened

Serial execution of tasks