iOS development-Multi-thread gcd (Grand central Dispatch)

Source: Internet
Author: User

Grand Central Dispatch (GCD) is a powerful way to perform multithreaded tasks, whether you are asynchronous or synchronous at the time of the callback, and you can optimize the application to support multi-core processors and other symmetric multi-processing systems. In the process of development, only the tasks to be executed and added to the appropriate dispatch queue, GCD can generate the necessary threads and plan to perform the task. The Dispatch queue is simpler and more efficient in implementing multithreaded tasks that meet your needs. Dispatch queue generally there are three ways, such as:

Serial execution of the first-out, concurrent is the concurrent execution of tasks, main belongs to the globally available quene; From the definition we can know serial in a queue, occupy a thread, concurrent occupy multiple threads, The exact quantity is determined by the system, and the order of execution is random.

Dispatch Quene Simple call:

    Two different ways to call a queue   dispatch_queue_t  quene = dispatch_queue_create ("Http://www.cnblogs.com/xiaofeixiang", NULL );    Asynchronous Call Execution    Dispatch_async (quene, ^{        NSLog (@ "Dispatch_async simple call-flyelephant");    });    Synchronous    Dispatch_sync (Quene, ^{        NSLog (@ "Synchronous execution-flyelephant");    });

The way to get the global queue and the home column, typically when updating the UI, is to use the main Dispatch queue, which is more commonly used in the global queue comparison practice:

    Get main Queue        dispatch_queue_t mainqueue =dispatch_get_main_queue ();    Gets the global Queue    dispatch_queue_t globalqueue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);        Dispatch_sync (Globalqueue, ^{        NSLog (@ "Dispatch_get_global_queue call-flyelephant");    });

Main use when you need to pay attention to the use of asynchronous, otherwise the page will be stuck, main and global is the whole object, do not need to be released, the system will automatically process;

    dispatch_queue_t mainqueue =dispatch_get_main_queue ();        Dispatch_async (Mainqueue, ^{        NSLog (@ "Dispatch_get_main_queue call-flyelephant");        Self.myimageview.image=[uiimage imagenamed:[nsstring stringwithformat:@ "thread2.jpg"];    });

The exact representation of the deferred task Dispatch_after,dispatch_after should be to add the task to the queue after a certain amount of time, and if the time accuracy is required, it needs to be changed according to the requirement:

    dispatch_time_t time = Dispatch_time (dispatch_time_now,10);    Dispatch_after (Time, globalqueue,^{        NSLog (@ "deferred execution");    });
Dispatch_apply its usage is like a for usage, you can specify the number of times the block executes. This function is useful when you want to perform the same block for all elements in an array, with simple usage, specifying the number of executions and the dispatch Queue, with an index in the block callback. You can then determine which element is currently being manipulated based on this index.
    dispatch_queue_t globalqueue = dispatch_get_global_queue (dispatch_queue_priority_default, 0);        Dispatch_apply (Globalqueue, ^ (size_t i) {            NSLog (@ "%zu", I);        });

Control look at the results will find the result is:

2015-02-11 22:54:29.602 threaddemo[1404:54736] 22015-02-11 22:54:29.602 threaddemo[1404:54734] 32015-02-11 22:54:29.602 threaddemo[1404:54707] 02015-02-11 22:54:29.603 threaddemo[1404:54736] 42015-02-11 22:54:29.603 THREADDEMO[1404:54734] 52015-02-11 22:54:29.603 threaddemo[1404:54707] 62015-02-11 22:54:29.603 ThreadDemo[ 1404:54736] 72015-02-11 22:54:29.603 threaddemo[1404:54707] 92015-02-11 22:54:29.603 ThreadDemo[1404:54734] 82015-02-11 22:54:29.602 threaddemo[1404:54733] 12015-02-11 22:54:29.603 threaddemo[1404:54707]  Dispatch_ After apply is executed

Global is the concurrent queue (Concurrent Dispatch queue), does not guarantee the order of execution, but the dispatch_apply function is synchronous, the execution process will cause the thread to stop here, we can use in an asynchronous thread Dispatch_ Apply function, change slightly:

    Dispatch_async (Globalqueue, ^{        dispatch_apply (Ten, Globalqueue, ^ (size_t i) {            NSLog (@ "%zu", I);        });    NSLog (@ "dispatch_apply after execution");

The result of the execution after the modification:

2015-02-11 22:58:24.321 threaddemo[1433:56214] dispatch_apply after execution 2015-02-11 22:58:24.321 ThreadDemo[1433:56285] 32015-02-11 22:58:24.321 threaddemo[1433:56282] 22015-02-11 22:58:24.323 threaddemo[1433:56285] 42015-02-11 22:58:24.321 threaddemo[1433:56284] 02015-02-11 22:58:24.323 threaddemo[1433:56282] 52015-02-11 22:58:24.321 threaddemo[1433:56283] 12015-02-11 22:58:24.323 threaddemo[1433:56285] 62015-02-11 22:58:24.323 ThreadDemo[ 1433:56284] 72015-02-11 22:58:24.323 threaddemo[1433:56282] 82015-02-11 22:58:24.323 ThreadDemo[1433:56283] 9

Dispatch queue paused with recovery, this basic nothing to say, just a call to the method:

    Suspension of    Dispatch_suspend (globalqueue);    Recovery    Dispatch_resume (globalqueue);

Dispatch_barrier_async: The previous task is executed before execution, and after the task is completed before it can be executed, the specific reference code:

    dispatch_queue_t queue = dispatch_queue_create ("Http://www.cnblogs.com/xiaofeixiang", dispatch_queue_concurrent);    Dispatch_async (queue, ^{        [Nsthread sleepfortimeinterval:1];        NSLog (@ "Dispatch_async-keso");    });    Dispatch_async (queue, ^{        [Nsthread sleepfortimeinterval:2];        NSLog (@ "dispatch_async-flyelephant");    });    Dispatch_barrier_async (queue, ^{         [Nsthread sleepfortimeinterval:3];        NSLog (@ "dispatch_barrier_async-Blog Park");    Dispatch_async (queue, ^{        [Nsthread sleepfortimeinterval:4];        NSLog (@ "dispatch_async-agile Thumb");    });

The results of the final execution are as follows, notice the observation time:

2015-02-11 23:08:40.668 threaddemo[1475:59699] dispatch_async-keso2015-02-11 23:08:41.664 ThreadDemo[1475:59698] dispatch_async-flyelephant2015-02-11 23:08:44.670 threaddemo[1475:59698] dispatch_barrier_async-Blog Park 2015-02-11 23:08:48.675 threaddemo[1475:59698] dispatch_async-Agile Thumb

 Dispatch_group:queue in dispatch_group_wait will wait for the previous task, if the previous task is time-consuming, then the thread is blocked, use needs to be cautious, and wait time can be specified in the time, in the example Dispatch_time_forever is always waiting, if it is used in OS X 10.8 or iOS 6 and later , DISPATCH group will be automatically managed by arc, and if it is earlier, it needs to be released manually.

    dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);    dispatch_group_t group = Dispatch_group_create ();        Dispatch_group_async (group, queue, ^{        NSLog (@ "group task One");        Dispatch_group_async (group, queue, ^{        NSLog (@ "group task");    });        Dispatch_group_wait (group, dispatch_time_forever);        NSLog (@ "group task completed");

  Dispatch_semaphore can be simply understood as semaphores, first create a semaphore, then wait for the callback to perform the task, to release after the task is performed, the main processing point is waiting for wait, the simple code is as follows:

    dispatch_semaphore_t Fd_sema = Dispatch_semaphore_create (Getdtablesize ()/2);    Wait for    dispatch_semaphore_wait (Fd_sema, dispatch_time_forever);      NSLog (@ "Dispatch_semaphore test");    Release    dispatch_semaphore_signal (Fd_sema);

Be asked to sleep, if there is a wrong place to write, a lot of mistakes ~

iOS development-Multi-thread gcd (Grand Central Dispatch)

Related Article

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.