Objetive-c Learning _GCD Learning Notes

Source: Internet
Author: User

GCD: A solution to the parallel operation of the Apple for multi-core

GCD automatically manages the life cycle of threads (create threads, schedule tasks, destroy threads)

Queue: Used to hold tasks (serial queue, parallel queue)

Task: What action to perform (synchronous, asynchronous)

Concurrency: Multiple tasks executing concurrently

Serial: Once a task is completed, the next task is performed

Global concurrency Queue (multiple bars)

Manually create a serial queue (1)

Main Queue (0)

requires 0

no new threads opened

No new thread is turned on

serial execution task

serial execution task

Async (Async)

Need more than one

have a new thread open

concurrent Execution of tasks

have a new thread open

Serial Execution tasks

No new threads opened

Serial Execution tasks

my memory method (just memory method): Think of the queue as having the ability to have new threads, but their limited ability cannot be opened indefinitely, and the task as the number of thread required. The number of final threads (quantities such as tables) is obtained based on demand and queue capacity.

Corresponding method:

Synchronous execution of tasks: Dispatch_sync (dispatch_queue_t queue, dispatch_block_t block);

Perform tasks asynchronously: Dispatch_async (dispatch_queue_t queue, dispatch_block_t block);

Get global Concurrent queue: Dispatch_get_global_queue (dispatch_queue_priority_default, 0);

Create serial queue: dispatch_queue_create (const char *label, dispatch_queue_attr_t attr); Queue name, queue properties (typically null)

Get the Home row: Dispatch_get_main_queue ();

Delay function:

(1) [Self performselector: (SEL) Withobject: (ID) Afterdelay: (nstimeinterval)];//object method, only to write a method on the current thread

(2) Dispatch_after (Dispatch_time (dispatch_time_t when, int64_t Delta), dispatch_queue_t queue, ^ (void) block)

Parameter description:

When can be set to 0 can also be used Dispatch_time_now, indicating from now on

Delta General Incoming (int64_t) (x * nsec_per_sec) where x indicates how many seconds after

Nsec_per_sec Print Out is 1000000000

dispatch_time_t is actually unsigned long long

Int64_t is actually a long long

Single-Case mode:

+ (ID) sharemanager

{

Static Mymanager * staticinstance = nil;

Static dispatch_once_t Oncetoken;

Dispatch_once (&oncetoken, ^{

Staticinstance = [[Self alloc]init];

});

return staticinstance;

}

Queue Group Use steps: (take action after multiple time-consuming operations are completed)

1. Create a queue group: dispatch_group_t group = Dispatch_group_create ();

2. Create a group task to add to the group: Dispatch_group_async (dispatch_group_t Group, dispatch_queue_t queue, ^ (void) block);

3. Wait until the tasks in the queue group are complete before performing other operations: Dispatch_group_notify (dispatch_group_t Group, dispatch_queue_t queue, ^ (void) block);

Note points and knowledge points:

(1) If the use of non-arc, the creation of the queue is also to be released, in general function name with Create/copy/new/retain, you need to do not need to use this data when the release. However, the data type of the CF (Core Foudation) is still being release in the ARC environment

(2) The sync function cannot be placed in the main thread, adding a task to the main queue (will be stuck) in the primary thread. Because the serial queue is executing a task, it does not perform the next

(3) GCD has provided a global concurrent queue for the entire application to use, do not need to create manually; serial queues are created for themselves.

(4) Removal queue FIFO principle: FIFO, LIFO

Objetive-c Learning _GCD Learning Notes

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.