Grand Central Dispatch (GCD)

Source: Internet
Author: User
Grand Central Dispatch (GCD) is a new solution developed by Apple for multi-core programming. It was first launched in Mac OS X 10.6 snow leopard and recently introduced to ios4.0. GCD is an efficient and powerful technology that replaces technologies such as nsthread. GCD can fully handle asynchronous programming issues such as data locking and resource leakage. GCD can accomplish many things, but this article only focuses on some basic knowledge required to implement multithreading in IOS applications. Before getting started, you must understand that what you want to provide to the GCD queue is Code It is used to schedule the operation on the queue created by the system or the user. Declare a queue
A user-created queue is returned as follows:

Dispatch_queue_t myqueue = dispatch_queue_create ("com. iphonedevblog. Post", null );

The first parameter identifies the queue, and the second parameter defines the queue parameters (currently not supported, so null is passed in ). Execute a queue
The following code is passed in asynchronously:

Dispatch_async (myqueue, ^ {[self dosomething] ;});

First, pass in the created queue, and then provide the code block that is run by the queue. Declare and execute a queue
If you do not need to retain the reference of the queue to be run, you can use the following code to implement the previous functions:

Dispatch_async (dispatch_queue_create ("com. iphonedevblog. Post", null), ^ {[self dosomething];});

Pause a queue
To suspend a queue, you can call the following code. Pausing a queue stops all Code related to the queue.

Dispatch_suspend (myqueue );

Restore a queue
Do not forget to restore a paused queue. The pause and recovery operations are similar to the retain and release operations in memory management. Calling dispatch_suspend will increase the pause count, while dispatch_resume will decrease. The queue starts to run only when the pause count turns to zero. Dispatch_resume (myqueue ); Run code in the main thread from the queue
Some operations cannot be run in the asynchronous queue, so they must be run in the main thread (each application has one. Ui plotting and any call to nsicationicationcenter must be performed on the master thread length. An example of accessing the main thread and Running code in another queue is as follows:

Dispatch_sync (dispatch_get_main_queue (), ^ {[self dismissloginwindow];});

Note that dispatch_suspend (and dispatch_resume) does not work on the main thread.

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.