GCD serial concurrent queue

After studying this article, you will have a better understanding of the following knowledge:

  • queue
  • Serial queue
  • Concurrent queue
  • GCD global queue
  • GCD home team
  • Creating a serial queue
  • Creating concurrent queues

GCD serial concurrent queue
Paste_Image.png

queue

No matter what the queue is, it must be the FIFO queue, that is, fifo.
so remember, either the serial queue (SerialQueue) or the concurrent queue (ConcurrencyQueue) is the FIFO queue. It means that the tasks must be executed one by one, in the first and first out sequence.
serial queue: when creating a queue, passing arguments DISPATCH_QUEUE_SERIAL indicates the creation of a serial queue. Tasks are executed one by one, and the next task will continue until the previous task has been completed. Serial execution does not mean synchronous execution. Be sure to distinguish between
concurrent queues: when creating queues, passing arguments DISPATCH_QUEUE_CONCURRENT indicates the creation of concurrent queues. Concurrent queues create threads as much as possible to perform tasks. Concurrent queue tasks will execute the task according to the order of the team, but what tasks to complete is uncertain.

Queue type

Apple provides the following queues:

Global queue: Apple’s predefined global concurrent queue can only be obtained by the API provided by apple, and can be set priority.
home queue: when the application starts, it automatically creates a serial queue associated with the main thread. We can also get it and cannot create it manually.
manually creates the serial queue
and creates the concurrent queue manually

Global queue

The global queue of the second parameters used to set priorities, only the following four options:
DISPATCH_QUEUE_PRIORITY_HIGH 2
DISPATCH_QUEUE_PRIORITY_DEFAULT 0
DISPATCH_QUEUE_PRIORITY_LOW (-2)
DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN
“we usually use the default option, so a lot of time to see all the 0. Let’s look at the creation of four tasks to perform an asynchronous concurrent queue:
dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog (@ 1);
});
dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
/ /
sleep sleep for 2 seconds (2);
NSLog (@ “2”);
});

Dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{/ sleep for 3 seconds (1); sleep NSLog (@ 3);});

Print results are as follows:

GCD serial concurrent queue
Paste_Image.png

three tasks are performed in three threads, with only three thread numbers.
in the code above, we add three concurrent tasks in the queue, which is the direct execution of 1 tasks, 2 tasks in asynchronous execution during sleep for 2 seconds, 3 tasks in asynchronous execution by sleep 1, the result of task task 2 completed prior to 3. Explain that concurrent execution of tasks does not require waiting for other tasks to be done first. The three tasks are mutually exclusive!
of course, such as the global queue can specify task priority,
: dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSLog (@ 4);
});
dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSLog (@ 3);
});
dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog (@ 2);
});
dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSLog (@ 1);
});

GCD serial concurrent queue
Paste_Image.png

The
priority runs from high to low, and the task has the highest priority, so it is prioritized in a concurrent queue.

Main queue

The home queue is a queue that is created by the system and is associated with the main thread when the application is started. We can only get the home team column through system API, so we can’t create it manually. Let’s take a look at the sequence of execution of the home team’s serial queue:

Dispatch_async (dispatch_get_main_queue (sleep), ^{(2); NSLog (@ "main 1");}); dispatch_async (dispatch_get_main_queue (NSLog), ^{(@ "main 2");}); dispatch_async (dispatch_get_main_queue (sleep), ^{(1); NSLog (@ "main 3");});

The running results are as follows:

GCD serial concurrent queue
Paste_Image.png

print the results can be seen from the order of execution in order to carry out the team. Although the task 1 sleep 2 seconds to perform, and other tasks can only wait until the task 1 completed, in order to inherit the task of 2, in the task 2 complete execution, to perform the task 3.
from the print results can see that the thread number is fixed, the instructions are performed in the same thread, and this thread is the main thread. Tasks can only be executed one by one.

Creating a serial queue

Create a queue through the dispatch_queue_create function, a parameter that is a string of C language, the label of the queue, which is the name, and the parameter two, which specifies whether the serial queue or the concurrent queue.
DISPATCH_QUEUE_SERIAL: create serial queue (also known as NULL)
DISPATCH_QUEUE_CONCURRENT: create concurrent queue

Dispatch_queue_t serialQueue = dispatch_queue_create (serial_queue, DISPATCH_QUEUE_SERIAL); dispatch_async (serialQueue, NSLog ^{(@ S1);}); dispatch_async (serialQueue, sleep ^{(2); NSLog (@ S2);}); dispatch_async (serialQueue, sleep ^{(1); NSLog (@ S3 ");});

Print results are as follows:

GCD serial concurrent queue
Paste_Image.png

The serial queue created by
runs only on one thread.

Creating concurrent queues

Create a queue through the dispatch_queue_create function, a parameter that is a string of C language, the label of the queue, which is the name, and the parameter two, which specifies whether the serial queue or the concurrent queue.
DISPATCH_QUEUE_SERIAL: create serial queue (also known as NULL)
DISPATCH_QUEUE_CONCURRENT
: create a concurrent queue to look at whether the serial queue need to wait for the task execution is completed, the next task to start:

Dispatch_queue_t concurrencyQueue = dispatch_queue_create (com.huangyibiao.concurrency-queue, DISPATCH_QUEUE_CONCURRENT); dispatch_async (concurrencyQueue, NSLog ^{(@ S1);}); dispatch_async (concurrencyQueue, sleep ^{(2); NSLog (@ S2);}); dispatch_async (concurrencyQueue, sleep ^{(1); NSLog (@ S3 ");});

Print results:

GCD serial concurrent queue
Paste_Image.png

from the print results can be seen, the task in the three sub thread execution, and without interference, without waiting for other tasks to complete, you can perform separately!