Common multithreading schemes in ios

GCD common functions

  • GCD has two functions for performing tasks
    • Perform tasks synchronously

    • dispatch_sync(dispatch_queue_t queue, dispatch_block_t block);

      • The queue, queue
      • Block: task
    • Perform tasks asynchronously

    • dispatch_async(dispatch_queue_t queue, dispatch_block_t block);

    • GCD source: github.com/apple/swift…

The GCD queues

  • GCD queues can be divided into two main types
    • Concurrent Dispatch Queue

      • You can allow multiple tasks to execute concurrently (simultaneously) (automatically start multiple threads to execute tasks at the same time), not really at the same time, but just switch between executing a and B in advance of multiple tasks.
      • Concurrency is only available with asynchronous (dispatch_async) functions
    • Serial Dispatch Queue

      • Allow tasks to be executed one after another (after one task is completed, proceed to the next)

Confusing terms

  • There are four confusing terms: synchronous, asynchronous, concurrent, and serial
    • Synchronization and asynchrony affect whether new threads can be started

      • Synchronization: Executes tasks in the current thread without the ability to start a new thread
      • Asynchrony: Performs tasks in a new thread, with the ability to start a new thread
    • Concurrency and serialization affect: how tasks are executed

      • Concurrency: Multiple tasks are executed concurrently (simultaneously)
      • Serial: After one task is executed, the next task is executed

The execution effect of various queues

  • In a serial queue (no new threads are started), do not add new tasks to a task synchronously, as this can cause a deadlock.
  • Adding tasks to the current serial queue using sync will lock the current serial queue (deadlock)

// Dispatch_sync and dispatch_async are used to control whether or not to start new threads /** queue types, which determine how tasks are executed (concurrent, serial) 1. Serial queue 3. Main queue (also a serial queue) */

The use of queue groups

  • Consider: How to use GCD to achieve the following functions
    • Asynchronously execute task 1 and task 2 concurrently
    • After task 1 and task 2 are completed, return to the main thread and perform task 3

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {// create a dispatch_group_t group = dispatch_group_create(); Dispatch_queue_t queue = dispatch_queue_create("my_queue", DISPATCH_QUEUE_CONCURRENT); Dispatch_group_async (group, queue, ^{for (int I = 0; i < 5; I++) {NSLog (@ "task 1 - % @", [NSThread currentThread]); }}); dispatch_group_async(group, queue, ^{ for (int i = 0; i < 5; I++) {NSLog (@ "task 2 - % @", [NSThread currentThread]); }}); // After the previous task is completed, // dispatch_group_notify(group, queue, ^{dispatch_async(dispatch_get_main_queue(), ^{for (int I = 0; i < 5; I++) {/ / NSLog (@ "task 3 - % @", [NSThread currentThread]); / / / /}}); / /}); // dispatch_group_notify(group, dispatch_get_main_queue(), ^{ // for (int i = 0; i < 5; I++) {/ / NSLog (@ "task 3 - % @", [NSThread currentThread]); / / / /}}); dispatch_group_notify(group, queue, ^{ for (int i = 0; i < 5; I++) {NSLog (@ "task 3 - % @", [NSThread currentThread]); }}); dispatch_group_notify(group, queue, ^{ for (int i = 0; i < 5; I++) {NSLog (@ 4 - % @ "task", [NSThread currentThread]); }}); }Copy the code