Simple understanding of IOS Grand Central Dispatch (GCD)

Source: Internet
Author: User
Tags gcd

Introduction:

The full name of GCD is the Grand Central Dispatch, an API that Apple has published in iOS4.0 to handle concurrency. Its purpose is to improve the processor's multi-core computing capabilities.
GCD is a bit like nsoperationqueue, which allows programs to split a task into multiple single tasks and commit to a work queue to execute concurrently or serially, but GCD is more efficient than nsopertionqueue.



How the GCD works:

GCD works by allowing programs to queue in parallel to specific tasks, scheduling them to perform tasks on any available processor core, depending on the available processing resources.
A task can be a function or a block. The bottom of the GCD is still threaded, but this allows the programmer to focus on the details of the implementation.
The FIFO queue in the GCD is called the dispatch queue, which guarantees that advanced tasks are executed first .



Resources:

1:blocks and the use of the dispatch queue

http://liwpk.blog.163.com/blog/static/363261702012413103111749/

2: Using GCD

http://blog.devtang.com/blog/2012/02/22/use-gcd/

3:GCD (Grand Central Dispatch) tutorial

Http://www.dreamingwish.com/dream-2012/gcdgrand-central-dispatch%E6%95%99%E7%A8%8B.html

The GCD multithreading model in 4:ios

Http://www.cnblogs.com/astin/archive/2012/08/02/2619752.html

5:ios Multi-threaded programming Grand Central Dispatch (GCD) introduces and uses

http://blog.csdn.net/totogo2010/article/details/8016129


Use:


Benefits and considerations for using GCD:

1. The ability to efficiently utilize processor multicore operations by using GCD

2. Complex and long computing tasks can be assigned to other threads through GCD to complete the task.

3. If there is no special requirement, threading should not be introduced to increase the complexity of the program

4. Be cautious about threading blocking.


Understanding Dispatch Queue (Dispatch queue):

The Dispatch Queue is divided into the following three types:


Serial Queue (serial queue):
Serial Queue They are each executed synchronously (rarely used)


Concurrent Queue (parallel queue):
Also known as the global dispatch queue, multiple tasks can be executed concurrently, but the order in which the execution is completed is random.


Main Queue (home row):
It is a globally available serial queue that performs tasks on the main thread of the application. Updates at the UI level must be performed through the main thread, or there will be many unpredictable problems.





GCD's API Introduction and Usage scenarios:




Dispatch_barrier_async

Tasks assigned by barrier can guarantee that the same queue is passed before the task is executed, when the task assigned by barrier is executed, when the task assigned by barrier is also completed, To continue with the next task.

The sample code is as follows:

[CSharp]View Plaincopy
  1. dispatch_queue_t queue = dispatch_queue_create ("wwww", dispatch_queue_concurrent);
  2. Dispatch_async (Queue, ^{
  3. [Nsthread sleepfortimeinterval:0.2];
  4. NSLog (@"Task 1");
  5. });
  6. Dispatch_async (Queue, ^{
  7. [Nsthread sleepfortimeinterval:0.2];
  8. NSLog (@"Task 2");
  9. });
  10. Dispatch_barrier_async (Queue, ^{
  11. NSLog (@"etc Task1, task2 execution after completion I will execute");
  12. [Nsthread sleepfortimeinterval:0.2];
  13. });
  14. Dispatch_async (Queue, ^{
  15. [Nsthread sleepfortimeinterval:0.2];
  16. NSLog (@"because it is in the barrier task after the assignment, so I will wait until the barrier task is completed before I execute");
  17. });
  18. Dispatch_release (queue);

Execution Result:

2013-05-29 14:30:47.677 Gcddemo[4630:1b03] Task 2

2013-05-29 14:30:47.677 gcddemo[4630:1303] Task 1

2013-05-29 14:30:47.679 gcddemo[4630:1303] wait Task1, Task2 after execution is complete I'm going to do it .

2013-05-29 14:30:48.082 gcddemo[4630:1303] because at the time of being assigned Barrier Task back , so I'll wait . Barrier I'm not going to execute the task until it's done .


Note: You must ensure that tasks are executed sequentially in the same queue, and if there are multiple queues at the same time, there will be no effect.

Sequential execution in multiple queues requires the semaphore to be queued and allowed to schedule an orderly dispatch task. Enter semaphorebelow!





GCD Memory Management:

GCD's memory management controls the retention and deallocation of variables through Dispatch_retain and dispatch_release.

However, after iOS 6, GCD memory management can be fully delivered to arc.

If your project needs to be iOS5.0, you still need to manage it manually.

For more information, please refer to the following StackOverflow

Http://stackoverflow.com/questions/8618632/does-arc-support-dispatch-queues

Simple understanding of IOS Grand Central Dispatch (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.