Multithreading---GCD

Source: Internet
Author: User
Tags gcd

One, what is GCD?

GCD (Grand Center Dispatch), Grand Central Dispatch. A solution for multi-core programming developed by Apple. In all of the iOS implementations of multithreading (Nsthread,nsoperation&nsoperationqueue, GCD), GCD should be the most attractive, because GCD itself is the solution Apple proposes for multi-core parallel computing. GCD automatically uses more CPU cores (such as dual-core, quad-core) when working, and GCD automatically manages the thread's life cycle (creating threads, scheduling tasks, destroying threads). GCD is based on the C language, so when we write GCD related code, we face the function, not the method. If you use GCD, the thread is completely managed by the system, we do not need to write thread code. Simply define the tasks you want to perform and add them to the appropriate dispatch queue (dispatch queue). GCD is responsible for creating threads and dispatching your tasks, and the system provides thread management directly.

Two or three kinds of multithreading advantages and disadvantages?

1.NSThread:

Pros: Nsthread? Two lighter than the others; Cons: Need? Self-managed thread's lifecycle, thread synchronization. Thread synchronization to the data lock will have? A certain amount of system overhead;

2.nsoperation&nsoperationqueue:

Advantages: No need to close? Heart thread management, data synchronization of things, you can put the fine power in the self-need to practice the operation of the line. The cocoaoperation related class is nsoperation,nsoperationqueue. Nsoperation is an abstract class that makes it necessary to use it? Subclasses, can implement it or make it a definition of two? Subclasses: Nsinvocationoperation and Nsblockoperation. Create an object of the Nsoperation subclass, add the object to the Nsoperationqueue queue?

3.GCD:

Is the Grand Central Dispatch (GCD) developed by Apple? A multi-core programming solution? method. It can be used after the iOS4.0 begins. GCD? An alternative to technologies such as Nsthread, Nsoperationqueue,nsinvocationoperation, and so on. High efficiency and strong technology. Now the iOS system has been upgraded to 6, so do not use the heart of the technology can not be used.

Three, Concept:

There are 2 core concepts in GCD

(1) Task: what action to perform

(2) Queue: for storing tasks

The use of GCD is 2 steps: (1) Customizing Tasks (2) determine what you want to do

Adding tasks to the queue, GCD automatically takes the tasks from the queue and puts them in the corresponding thread

Tip: The removal of a task follows the FIFO principle of the queue: first-in, last-out

Multithreading---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.