Title description:

Q: Whether the GCD is synchronous or asynchronous will enable multi-threading

A: Synchronization does not start new threads, asynchrony does.

This is not difficult, basic is a must. Verify with code that synchronization creates new threads in both serial and concurrent queues

Verification code:

    dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue".DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t conQueue = dispatch_queue_create("conQueue".DISPATCH_QUEUE_CONCURRENT);

    NSLog(@"(1). = = = = = % @"[NSThread currentThread]);
    dispatch_sync(serialQueue, ^{
      NSLog(@"(2). = = = = = % @"[NSThread currentThread]);
    });
    dispatch_sync(conQueue, ^{
      NSLog(@"(3). = = = = = % @"[NSThread currentThread]);
    });
Copy the code

Output result:

(1). = = = = ="NSThread: 0x2837f6f00>{number = 1, name = main}
(2). = = = = ="NSThread: 0x2837f6f00>{number = 1, name = main}
(3). = = = = ="NSThread: 0x2837f6f00>{number = 1, name = main}
Copy the code

You can see that synchronization does not create new threads. Of course the problem will not be so simple to end.


Q: Does asynchrony always start a new thread?

A: No, asynchrony does not create new threads in the main queue. It creates new child threads in both serial and concurrent queues

Verification code:

    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue".DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t conQueue = dispatch_queue_create("conQueue".DISPATCH_QUEUE_CONCURRENT);

    NSLog(@"(1). = = = = = % @"[NSThread currentThread]);
    dispatch_async(serialQueue, ^{
      NSLog(@"(2). = = = = = % @"[NSThread currentThread]);
    });
    dispatch_async(conQueue, ^{
      NSLog(@"(3). = = = = = % @"[NSThread currentThread]);
    });
    dispatch_async(mainQueue, ^{
      NSLog(@"(4). = = = = = % @"[NSThread currentThread]);
    });
Copy the code

Output result:

(1). = = = = ="NSThread: 0x2800a6f00>{number = 1, name = main}
(2). = = = = ="NSThread: 0x2800ca5c0>{number = 3, name = (null)}
(3). = = = = ="NSThread: 0x2800ca5c0>{number = 3, name = (null)}
(4). = = = = ="NSThread: 0x2800a6f00>{number = 1, name = main}
Copy the code

(1) No new thread was started in the asynchronous main queue. (2) No new thread was started in the asynchronous main queue. (3) No new thread was started in the asynchronous main queue.

Looking at (2) and (3) again, you see that a new thread has indeed been created. However, a closer look at the thread number shows that the number corresponding to (2) and (3) are both 3. That is, these two asynchronous actions only create a new thread. Wouldn’t it be common sense to create two different threads?

This is time and space to talk about GCD thread scheduling optimization.

When the GCD performs multiple asynchronous operations, it will weigh whether to create new threads based on time and space. Generally, each task starts one thread, which is the optimal time. However, too many threads will consume memory space. Therefore, GCD will automatically weigh the appropriate number of threads to allocate according to the task to achieve space and time optimization.

Of course, the specific GCD is how to do, may need to see the source code…

Here’s my summary of the question:

  • Synchronization: do not have the ability to start threads, must be serial execution of tasks
  • Asynchrony: Has the ability to start threads, but does not start new threads in the main queue. If n child threads are enabled on serial and concurrent queues, there is no guarantee that there will be n child threads after GCD optimization.