iOS development: Multithreading with GCD

Source: Internet
Author: User

In addition to the previous article on the nsthread thread, there is also a gcd (Grand Central Dispath), a new Apple-developed solution for multi-core programming, leveraging CPU resources to put all tasks into a single task queue for scheduling. GCD provides a global concurrency queue by default, which is used by the entire application and does not need to be created.

    • The difference between synchronous and asynchronous
      • Dispatch_sync Sync: Does not have the ability to open threads
      • Dispatch_async Async : Ability to open threads
    • how tasks are executed
      • concurrent queues: Multiple tasks are executed concurrently and can only be performed under one-step functions " Async"
      • Serial queue: Let the task run one after another

Note: commonly used in actual development it's dispatch_async.

Here are a few common queue usage

One: The most commonly used global concurrency queue:

        //Get global concurrency queueLet queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0)        //add a task to a queue and execute concurrentlyDispatch_async (queue) {(), Voidinch            //dosomething ... Task 1} dispatch_async (queue) {()-Voidinch            //dosomething ... Task 2} dispatch_async (queue) {()-Voidinch            //dosomething ... Task 3        }                //pausing a queuedispatch_suspend (queue)//Continue queueDispatch_resume (Queue)

Two: Serial queue, the tasks in the queue will not be executed concurrently, one task will be executed after the completion of another task

 //Create a serial queueLet Serialqueue = Dispatch_queue_create ("Dispatch_queue_name", nil)//add a task to a queue one task executes before another task is executedDispatch_async (Serialqueue) {() Voidinch            //dosomething ... Task 1} dispatch_async (Serialqueue) {()-Voidinch            //dosomething ... Task 2} dispatch_async (Serialqueue) {()-Voidinch            //dosomething ... Task 3}

Three: GCD communication between threads, usually used for sub-threads to request data, back to the main thread update UI

// inter-thread communication     0 inch            // Doing something on a child thread            inch                // update UI on main thread ...             })        }

Four: Thread-deferred execution

// thread-Deferred execution        3.0 = Dispatch_time (Dispatch_time_now, Int64 (second * Double (nsec_per_sec)))                //          3s back to the main thread to perform the task        inch                     }        //3s will open a new thread to execute the task        in the queue 0 inch                     }

Five: A one-time code, as the name implies, within the application lifecycle, block blocks will only be executed once.

  0         dispatch_once (            in// only once during the application life cycle         )      

Six: Queue groups: Some tasks are put into a queue group, and when all tasks are completed, the notify of the group is executed.

/ ********************************** Queue Group ******************************************/Let group=dispatch_group_create () let Groupqueue= Dispatch_get_global_queue (Dispatch_queue_priority_default,0) Dispatch_group_async (group, Groupqueue) {()-Voidinch            //Task 1} dispatch_group_async (Group, Groupqueue) {()-Voidinch            //Task 2} dispatch_group_async (Group, Groupqueue) {()-Voidinch            //Task 3} dispatch_group_notify (Group, Groupqueue) {()-Voidinch            //will wait for Task 1, Task 2, Task 3 execution after completion}

Finish!

iOS development: Multithreading with 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.