Understanding of IOS GCD serial and concurrent queues

Source: Internet
Author: User

dispatch_queue_t Serialqueue = Dispatch_queue_create ("com.lai.www", dispatch_queue_serial); Dispatch_async (Serialqueue,^{//NSLog (@"1"); Sleep (3); NSLog (@"1");    }); Dispatch_sync (Serialqueue,^{sleep (1); NSLog (@"2");    }); Dispatch_async (Serialqueue,^{NSLog (@"3");    }); Dispatch_sync (Serialqueue,^{sleep (5); NSLog (@"4");        }); Dispatch_async (Serialqueue,^{NSLog (@"5"); });

Look at the code above, what do you guess will output?  Anyway before today I would think that its output pits is not orderly, before always think of serial is an execution, good, the first is asynchronous, then asynchronous go, the second synchronization, synchronous execution, go to perform a third, and then ... The synchronization executes, then executes the next, and asynchronously does not know when the execution is done.

Haha, today I have to think about my own idea of SB. It's too self-righteous. As you can see, I can't understand the nature of the serial queue.

So what is a serial queue? What is a parallel queue?

    • Concurrent: tasks is dequeued in the FIFO order, but run concurrently and can finish on any order.

    • Serial: tasks execute one at a time in FIFO order

Concurrency: Tasks are removed from the sequence in a FIFO and run concurrently, and can be done in any order. It automatically turns on multiple threads and executes tasks simultaneously

Serial: The task executes in a FIFO from one sequence to the next. Schedule only one task at a time, and the tasks in the queue are executed one after the other (after a task is completed and the next task is executed) and only one thread is opened

In order to verify that only one thread in the serial queue performs the task:

dispatch_queue_t queue = dispatch_queue_create ("com.lai.www", dispatch_queue_serial);        for (int i = 0; i< 10;i++) {        //10 async        Dispatch_async (queue, ^{            NSLog (@ "%@--%d", [Nsthread CurrentThread], I );        });    }

We can see that the thread's address is the same, which confirms that the serial queue has only one thread to perform the task. Then we test the concurrency queue

dispatch_queue_t queue = dispatch_queue_create ("com.lai.www", dispatch_queue_concurrent);        for (int i = 0; i< 10;i++) {        //10 async        Dispatch_async (queue, ^{            NSLog (@ "%@--%d", [Nsthread CurrentThread], I );        });    }

It can be seen that there are multiple threads executing tasks in the concurrent queue.

Comparing the above two programs, we just created two different types of queue, but the results are very disparate, the concurrency will open multiple threads, the order of execution we have no control, as for that thread to execute that task is determined by the queue, which task is completed by the CPU and the operating system is determined, and the threads in the concurrent queue can be reused (the thread pool), which is understandable, but below:

dispatch_queue_t queue = dispatch_queue_create ("com.lai.www", Dispatch_queue_ CONCURRENT);          for (int0; i++) {        //  10 Asynchronous        Dispatch_sync (queue, ^{            NSLog (@ "%@--%d", [Nsthread CurrentThread], i);        });    

It is found that concurrency can only be valid under asynchronous functions, Name=main, and does not create new threads. All tasks are executed sequentially on the main thread.

Understanding of IOS GCD serial and concurrent queues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.