OC Development _ Class notes-multi-thread GCD

Source: Internet
Author: User
Tags gcd

I. Processes and Threads

Two, various queues!

1, Gcd:grand Central Dispatch

2. Serial Queue (Serial)

Youcan createAny number of serial queues, each of which performs the added task sequentially,A queue can only perform one task at a time(serial), but does not affect each queue and can be executed concurrently. The tasks in each queue run on a separate thread maintained by the respective serial queue.only one thread in a queue。

3. Parallel Queue (Concurrent)

Parallel queues aredo not allow yourself to create, there are three parallel queues in the system with different priority levels. Parallel queues still start tasks in the order in which they are added, but the latter task does not have to wait for the previous task to complete, but instead starts the next task immediately after starting the first one. As to how many tasks are allowed to run concurrently at the same timeSystem Decision。 Tasks run on separate threads provided to them by the parallel queue,How many threads must be maintained in a parallel queue to run concurrently。

4. Main dispatch queue (main dispatch queues)

The tasks in the main dispatch queue run in the applicationMain ThreadOn, so, if you want to modify the interface of the application, he is the only option 5, Global queue (Apple provides a global queue for easy multi-threading design forall apps are used together)

Three, queue of task processing

1, serial queue (? Dispatch_queue_serial) Synchronization tasks and asynchronous tasks: Dispatch_sync and Dispatch_async

      

# Serial Synchronous Execution Result: No new threads are created, executed sequentially

# serial asynchronous Execution Result: a new thread was created, but still executed in sequence

  

# serial first asynchronous, then synchronous results: The discovery synchronization is always the main thread, but also let the asynchronous execution to perform the synchronization again,

This indicates that a serial queue is executed sequentially , but does not affect the operation of the main thread

  

2, parallel queue (? Dispatch_queue_concurrent) Synchronization tasks and asynchronous tasks: Dispatch_sync and Dispatch_async

    

# Parallel Synchronous Execution Results: No new threads are created and sequential [same serial synchronization] is performed

# Parallel asynchronous execution results: new multiple threads are created , operations are executed out of order (there is, error prone!) If there are other tasks before the queue, wait until the previous task is completed.

Scene: does not affect the main thread, do not need to order the operation of the line!

  

# parallel synchronization is performed before asynchronous results are performed: The asynchronous execution is not performed until the synchronization is executed

# parallel executes asynchronously, then executes the result of the synchronization: the order has completely all sorts of chaos into!

  

3, Global queue (Dispatch_queue_priority_default): Development with this is the right dispatch_get_global_queue

  

# The global queue performs the synchronization first and then executes the asynchronous result: Actually the result is the same as the side effect

  

4. Main thread queue: dispatch_get_main_queue[ The sync operation added in the primary queue will never be taken.

  

Iv. Nested Synchronization Tasks

1, in the serial queue nested more than one synchronization task, will cause blocking!!

Five, a brief summary

1. The difference between parallel and serial task scheduling

  

OC Development _ Class notes-multi-thread GCD

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.